XCharts图表插件,LineChart折线图,删除折线点功能

XCharts图表插件,在折线图上删除点

一、前言
XCharts插件,在使用中发现,可以在折线图上添加点,移除所有点,但并没有移除单个点的功能。
二、思路
1,首先我们要知道,在折线图上添加的点,点的数据信息都存在了lineChart.series.GetSerie(0).data里面。
2,其次经过尝试,发现api里虽然没有直接给我们点的位置信息(x,y的值),但每个点都有一个label,给了我们label的位置信息(labelPosition)
3,然后我们要知道每个点是一个圆形,鼠标放上去,点会被放大,点被选中时的大小api中也给了。(selectSize)
4,删除点其实就是把此点在lineChart.series.GetSerie(0).data中的信息给移除掉
5,知道以上4点内容,相信大家都知道该咋解决了吧。
代码逻辑:
点击鼠标左键,获取鼠标点击的位置,用 鼠标点击位置x的值 减去 点标签位置的x值,并 取其绝对值(y值同理);用 此绝对值 与 点被选中时的大小 做比较。
如果 两个绝对值 都 小于 此点被选中时的大小,那么鼠标点中了,移除此点在lineChart.series.GetSerie(0).data中的信息。删除点成功。
三、代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using XCharts;
public class NewBehaviourScript : MonoBehaviour
{
public LineChart lineChart;
public float selectSize = 10f;
List serieDatas = new List();
// Start is called before the first frame update
void Start()
{
PointInit();
}

/// <summary>
/// 小圆点初始化
/// </summary>
void PointInit()
{
    lineChart.series.GetSerie(0).label.show = true;
    lineChart.series.GetSerie(0).label.textStyle.fontSize = -1;
    lineChart.series.GetSerie(0).symbol.selectedSize = selectSize;
}




/// <summary>
/// 移除小圆点
/// </summary>
void RemovePoint()
{
    if (Input.GetMouseButtonDown(0))
    {
        Vector2 mousePos = new Vector2(Input.mousePosition.x - Screen.width / 2f, Input.mousePosition.y - Screen.height / 2f);
        //Debug.Log("鼠标" + mousePos);

        foreach (var item in lineChart.series.GetSerie(0).data)
        {
            if (Mathf.Abs(mousePos.x - item.labelPosition.x) < selectSize && Mathf.Abs(mousePos.y - item.labelPosition.y) < selectSize)
            {
                Debug.Log("点中了");
                lineChart.series.GetSerie(0).data.Remove(item);
                return;
            }
        }

    }
}

// Update is called once per frame
void Update()
{
    RemovePoint();
}

}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hyt0626

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值