如何使用Chart.JS实现链接图表

yiytaume  于 2023-01-13  发布在  Chart.js
关注(0)|答案(1)|浏览(211)

我设法实现了一个随时间变化的价格图表,但现在我想根据项目类别筛选图表。我计划使用饼图来显示项目类别,当选择特定项目时,折线图将更新,仅显示与该项目关联的项目。
假设这是我的数据:

{"date": "06/01/2023 16:45", "item": "Apple", "cat": "Fruit", "price": "9.00"}
{"date": "06/01/2023 16:45", "item": "Orange", "cat": "Fruit", "price": "8.00"}
{"date": "06/01/2023 16:45", "item": "Pear", "cat": "Fruit", "price": "7.00"}
{"date": "06/01/2023 16:45", "item": "Water", "cat": "Drink", "price": "2.00"}
{"date": "06/01/2023 16:45", "item": "Soda", "cat": "Drink", "price": "3.00"}
{"date": "07/01/2023 16:45", "item": "Apple", "cat": "Fruit", "price": "9.50"}
{"date": "07/01/2023 16:45", "item": "Orange", "cat": "Fruit", "price": "8.50"}
{"date": "06/01/2023 16:45", "item": "Pear", "cat": "Fruit", "price": "7.50"}
{"date": "07/01/2023 16:45", "item": "Water", "cat": "Drink", "price": "2.50"}
{"date": "07/01/2023 16:45", "item": "Soda", "cat": "Drink", "price": "3.50"}

现在,如果我单击图表中的水果部分:

我想让折线图只显示苹果、橙子和梨的数据。我在Chart.JS 4文档中找不到任何关于如何制作链接图表的示例。有什么有用的示例可以分享吗?

mjqavswn

mjqavswn1#

可能还有其他一些方法 (甚至可能是更好的方法),但这就是我会这样做***快速和肮脏***。
onClick事件中,我将为另一个图表设置数据,而不仅仅是在第二个图表上调用update。(link to the documentation

    • 这里有一个演示,我将如何做到这一点:**
  • (单击饼图查看折线图)*
// Original Data
let dataSet = [
    {"date": "06/01/2023 16:45", "item": "Apple", "cat": "Fruit", "price": "9.00"},
    {"date": "06/01/2023 16:45", "item": "Orange", "cat": "Fruit", "price": "8.00"},
    {"date": "06/01/2023 16:45", "item": "Pear", "cat": "Fruit", "price": "7.00"},
    {"date": "06/01/2023 16:45", "item": "Water", "cat": "Drink", "price": "2.00"},
    {"date": "06/01/2023 16:45", "item": "Soda", "cat": "Drink", "price": "3.00"},
    {"date": "07/01/2023 16:45", "item": "Apple", "cat": "Fruit", "price": "9.50"},
    {"date": "07/01/2023 16:45", "item": "Orange", "cat": "Fruit", "price": "8.50"},
    {"date": "06/01/2023 16:45", "item": "Pear", "cat": "Fruit", "price": "7.50"},
    {"date": "07/01/2023 16:45", "item": "Water", "cat": "Drink", "price": "2.50"},
    {"date": "07/01/2023 16:45", "item": "Soda", "cat": "Drink", "price": "3.50"}
]

// pre split data
let drinkData = dataSet.filter(n => n.cat =='Drink')
let fruitData = dataSet.filter(n => n.cat =='Fruit')

let fruitDataSets = []
let drinkDataSets = []

let lineChartColors = ['#4BC0C0', '#FF9F40', '#9966FF']

// prepare Data for LineChart -- START
let formatedData = fruitData.reduce((p,c) => {
        if(!p[c.item]){
            p[c.item] = []
        }
        p[c.item].push({x: new Date(c.date), y: parseFloat(c.price), label: c.item})
        return p
    }, {});

Object.values(formatedData).forEach((n, i) => fruitDataSets.push({
  data: n, 
  label: n[0].label, 
  backgroundColor: lineChartColors[i],
  borderColor: lineChartColors[i]
}));
                
formatedData = drinkData.reduce((p,c) => {
        if(!p[c.item]){
            p[c.item] = []
        }
        p[c.item].push({x: new Date(c.date), y: parseFloat(c.price), label: c.item})
        return p
    }, {});

Object.values(formatedData).forEach((n, i) => drinkDataSets.push({
  data: n, 
  label: n[0].label, 
  backgroundColor: lineChartColors[i],
  borderColor: lineChartColors[i]
}));

// prepare Data for LineChart -- END

// create data for the pieChart
let preparedPieData = dataSet.reduce((p,c) => {         
      p[c.cat]++
      return p
  }, { Drink:0, Fruit: 0});

const pieData = {
    //  create labels for the pieChart
    labels: Object.keys(preparedPieData),
    datasets: [{
        data: Object.values(preparedPieData), 
        backgroundColor: ['#35A2EB', '#FF6283'], 
        borderWidth: 1,
     }],
};

let lineData = {
    datasets: [],
};

// PieChart Configuration
const config = {
    type: 'pie',
    data: pieData,
    options: {
        maintainAspectRatio: false,
        onClick: function(e, item){
          lineData.datasets = item[0].index == 0 ? drinkDataSets : fruitDataSets
          lineChart.update();
       }
    }
};

// LineChart Configuration
const config2 = {
    type: 'line',
    data: lineData,
    options: {
        maintainAspectRatio: false,
        scales: {
            x: {
                type: 'time',
                time: {
                    unit: 'week'
                }
            }
        }
    }
};

new Chart(
    document.getElementById('chart1'),
    config
);

let lineChart = new Chart(
    document.getElementById('chart2'),
    config2
);
<script src="//cdnjs.cloudflare.com/ajax/libs/Chart.js/3.9.1/chart.js"></script>     
<script src="//cdn.jsdelivr.net/npm/moment@^2"></script>
<script src="//cdn.jsdelivr.net/npm/chartjs-adapter-moment@^1"></script>
  

<div class="chart" style="float:left;height:184px; width:250px;">
    <canvas  id="chart1" ></canvas>
</div>

<div class="chart" style="float:left;height:184px; width:250px;">
    <canvas  id="chart2" ></canvas>
</div>

相关问题