如何在chart.js v3中显示具有不同数据集的timeseries配置的面积图?

zour9fqk  于 2021-09-13  发布在  Java
关注(0)|答案(1)|浏览(404)

我一直在努力实现 area 具有不同数据集的图表,我们通过提供一个最小值和最大值选项来自动生成标签,从而遵循timeseries图表的配置。这是我的密码:

var feronisData = [398, 445];
     var formattedDates = ["Feb 01", "Mar 20"];
     var colors = ['#5ba2df', '#e2ac00', '#59b110'];

      var data = {
      /* labels: formattedDates, */
        datasets: [{
        type: 'line',
        fill: 1,
          spanGaps: true,
          pointStyle: 'circle',
          label: 'Feronis',

          data: [{x: moment('2021-02-10 00:00:00.000').format('MMM DD'), y: 250}, {x: moment('2021-02-28 00:00:00.000').format('MMM DD'), y: 350}],

          borderColor: '#5ba2df',
          labels: [],
          borderWidth: 3,
          backgroundColor: 'rgba(0, 0, 0,0)',
        }, {
        type: 'line',
        fill: true,
        borderColor: '#2187f3',
backgroundColor: '#219634', 
borderWidth: 1,
          spanGaps: false,
          pointStyle: 'circle',
          label: 'Others',
          data: [{x: moment('2021-01-24 00:00:00.000').format('MMM DD'), y: 150},{x: moment('2021-02-04 00:00:00.000').format('MMM DD'), y: 300}, {x: moment('2021-03-24 00:00:00.000').format('MMM DD'), y: 450}],
          borderColor: '#e2ac00',
          labels:[],
          borderWidth: 3,
          backgroundColor: 'rgba(0, 0, 0,0)',
        }]
      };
      var options = {
        showLines: true,
        layout: {
          padding: {
            left: 0,
            right: 0,
            top: 0,
            bottom: 80
          }
        },
        elements: {
          point: {
            radius: 1,
            hoverRadius: 5,
          }
        },
        hover: {
          mode: 'index',
          intersect: false
        },
         responsive: true,
        tooltips: {
          enabled: true,
          mode: 'index',
          intersect: false,
          yAlign: 'right',
        },
        scales: {
        x : {
        type: 'time',
        min: 'Jan 01',
        max: 'Jul 01',
        time: {
          unit: 'day',
          displayFormats: {
            day: 'MMM DD'
          }
        }
          /* xAxes: [{
            type: 'time',
            time: {
            unit: 'day',
            unitStepSize: 10,
            format: 'MMM DD',
              displayFormats: {
                'day': 'dddd',
                                'week': 'MMM DD',
                'month': 'MMM DD'
              },
              min: 'JAN 01',
                max: 'JUL 01'
            },
            display: true,
            interval: 5,
            intervalType: "month",
            gridLines: {
              drawOnChartArea: false,
            }
          }], */
      },
      y: {
        stacked: true,
      }
      }}
  this.chart = new Chart('canvas', {
        responsive: true,
        data: data,
        options: options
      });

这里的小提琴上也有同样的功能:https://jsfiddle.net/erdfs37h/
Configuation is backend不会向我提供任何标签集,作为每个数据集中显示的标签数组。相反,我们直接依赖于 data 以这种格式提供值的数组 [{x: 'xItem', y: 'someValueToPlot'}] 注意:实际上,我将使用react.js来呈现它,其中我的公共组件根据这些配置生成图表,但它几乎类似于我在fiddle链接上显示的内容。
由于我们也在使用timeseries配置,您能指出我在这里缺少的任何具体内容吗?
目前,我认为可以通过为每个数据集添加这些关键字段来实现解决方案

type: 'line',
    fill: {
      target: 'origin',
      above: 'rgb(123, 43, 54)', // Area will be reddish above the origin
      below: 'rgb(65, 47, 231)' // And blue below the origin
    },
9jyewag0

9jyewag01#

您的问题是您为提供了重复的密钥 backgroundColor 在副本中,您将其设置为完全透明的颜色,这就是为什么它不显示数据集配置中的标签数组是红色的,因为您使用的是对象表示法
例子:

var feronisData = [398, 445];
var formattedDates = ["Feb 01", "Mar 20"];
var colors = ['#5ba2df', '#e2ac00', '#59b110'];

var data = {
  /* labels: formattedDates, */
  datasets: [{
    type: 'line',
    fill: 1,
    spanGaps: true,
    pointStyle: 'circle',
    label: 'Feronis',

    data: [{
      x: moment('2021-02-10 00:00:00.000').format('MMM DD'),
      y: 250
    }, {
      x: moment('2021-02-28 00:00:00.000').format('MMM DD'),
      y: 350
    }],

    borderColor: '#5ba2df',
    borderWidth: 3,
  }, {
    type: 'line',
    fill: true,
    borderColor: '#2187f3',
    backgroundColor: '#219634',
    borderWidth: 1,
    spanGaps: false,
    pointStyle: 'circle',
    label: 'Others',
    data: [{
      x: moment('2021-01-24 00:00:00.000').format('MMM DD'),
      y: 150
    }, {
      x: moment('2021-02-04 00:00:00.000').format('MMM DD'),
      y: 300
    }, {
      x: moment('2021-03-24 00:00:00.000').format('MMM DD'),
      y: 450
    }],
  }]
};
var options = {
  showLines: true,
  layout: {
    padding: {
      left: 0,
      right: 0,
      top: 0,
      bottom: 80
    }
  },
  elements: {
    point: {
      radius: 1,
      hoverRadius: 5,
    }
  },
  hover: {
    mode: 'index',
    intersect: false
  },
  responsive: true,
  plugins: {
    tooltip: {
      enabled: true,
      mode: 'index',
      intersect: false,
      yAlign: 'right',
    },
  },
  scales: {
    x: {
      type: 'time',
      min: 'Jan 01',
      max: 'Jul 01',
      time: {
        unit: 'day',
        displayFormats: {
          day: 'MMM DD'
        }
      }
    },
    y: {
      stacked: true,
    }
  }
}
this.chart = new Chart('canvas', {
  responsive: true,
  data: data,
  options: options
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.4.1/chart.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/moment@2.27.0"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chartjs-adapter-moment/1.0.0/chartjs-adapter-moment.min.js"></script>
<canvas style="height: 400px;" id='canvas' />

您可能还想查看迁移指南,因为正如我所看到的,您的工具提示也配置错误:https://www.chartjs.org/docs/master/getting-started/v3-migration.html

相关问题