钉钉小程序使用F2绘制对比折线图,以及处理重复数据

效果:

在这里插入图片描述
踩过无数坑之后,终于实现了这个效果,直接上代码吧。

  arrayUnqiue(arr, name) { // 传入参数 数组, 对象的名称(即对象数组里的属性)
    let hash = {}
    return arr.reduce((item, next) => {
      // eslint-disable-next-line
      hash[next[name]] ? '' : hash[next[name]] = true && item.push(next)
      return item
    }, [])
  },
onInitChart1(F2, config) {
    //处理数据
    let a = this.data.biologyList
    let arr = this.arrayUnqiue(a,'swTimeDate')
    const data = [];
    for (let i = 0; i < arr.length; i++) {
      let qx = Number(arr[i].qxValue)
      let obj = {
        value: qx,
        type: '情绪值',
        date: arr[i].swTimeDate
      }
      data.push(obj)
      let tl = Number(arr[i].tlValue)
      let obj1 = {
        value: tl,
        type: '体力值',
        date: arr[i].swTimeDate
      }
      data.push(obj1)
      let zl = Number(arr[i].zlValue)
      let obj2 = {
        value: zl,
        type: '智力值',
        date: arr[i].swTimeDate
      }
      data.push(obj2)
    }
    //生成折线图
    const chart = new F2.Chart(config);
    chart.source(data);
    chart.scale('date', {
      type: 'timeCat',
      tickCount: 3
    });
    chart.scale('value', {
      tickCount: 5
    });
    chart.axis('date', {
      label: function label(text, index, total) {
        // 只显示每一年的第一天
        const textCfg = {};
        if (index === 0) {
          textCfg.textAlign = 'left';
        } else if (index === total - 1) {
          textCfg.textAlign = 'right';
        }
        return textCfg;
      }
    });
    chart.tooltip({
      custom: true, // 自定义 tooltip 内容框
      onChange: function onChange(obj) {
        const legend = chart.get('legendController').legends.top[0];
        const tooltipItems = obj.items;
        const legendItems = legend.items;
        const map = {};
        legendItems.forEach(function (item) {
          map[item.name] = _.clone(item);
        });
        tooltipItems.forEach(function (item) {
          const name = item.name;
          const value = item.value;
          if (map[name]) {
            map[name].value = value;
          }
        });
        legend.setItems(_.values(map));
      },
      onHide: function onHide() {
        const legend = chart.get('legendController').legends.top[0];
        legend.setItems(chart.getLegendItems().country);
      }
    });
    chart.line().position('date*value').color('type');
    chart.render();
  },
<view class="f2-chart" a:if={{bloodList[0].cityNo}}>
  <f2 onInit="onInitChart" onInit="{{onInitChart}}" class="container" style="height: 300px;"></f2>
    <view style="margin-top:10rpx">
    血压折线图
  </view>
</view>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!关于 WPF C# 使用绘图控件将现有数据绘制折线图,并在鼠标点击折线图上的点时返回值,以下是一个简单的示例: 首先,我们需要在 XAML 定义一个 Canvas 控件,用于绘制折线图: ```xml <Canvas x:Name="canvas" Width="400" Height="300" Background="White" MouseLeftButtonDown="canvas_MouseLeftButtonDown" /> ``` 然后,在 C# 代码,我们可以使用 Polyline 控件绘制折线图,并在 MouseLeftButtonDown 事件获取鼠标点击的坐标,计算对应的值并返回: ```csharp private void DrawLineChart(List<double> data) { // 计算坐标系的大小和位置 double xMin = 0; double xMax = data.Count - 1; double yMin = data.Min(); double yMax = data.Max(); double xRange = xMax - xMin; double yRange = yMax - yMin; double xScale = canvas.ActualWidth / xRange; double yScale = canvas.ActualHeight / yRange; // 创建折线图的路径 Polyline polyline = new Polyline(); polyline.Stroke = Brushes.Blue; polyline.StrokeThickness = 2; for (int i = 0; i < data.Count; i++) { double x = i * xScale; double y = (yMax - data[i]) * yScale; polyline.Points.Add(new Point(x, y)); } // 将路径添到 Canvas 控件 canvas.Children.Clear(); canvas.Children.Add(polyline); } private void canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { // 获取鼠标点击的坐标 Point mousePosition = e.GetPosition(canvas); // 计算对应的值 double xMin = 0; double xMax = data.Count - 1; double yMin = data.Min(); double yMax = data.Max(); double xRange = xMax - xMin; double yRange = yMax - yMin; double xScale = canvas.ActualWidth / xRange; double yScale = canvas.ActualHeight / yRange; double xValue = mousePosition.X / xScale; double yValue = yMax - mousePosition.Y / yScale; // 返回值 MessageBox.Show(string.Format("X={0:F2}, Y={1:F2}", xValue, yValue)); } ``` 在这个示例,我们假设 data 是一个包含数据的 List<double>。在 DrawLineChart 方法,我们计算出折线图每个点的坐标,并使用 Polyline 控件绘制折线图。在 canvas_MouseLeftButtonDown 方法,我们获取鼠标点击的坐标,并计算出对应的值,然后使用 MessageBox 显示值。 希望这个示例对您有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值