【问题标题】:How can I get multiple charts(bar and line) with ng2-charts?如何使用 ng2-charts 获得多个图表(条形图和折线形图)?
【发布时间】:2019-09-11 09:24:27
【问题描述】:

我有条形图,我想在这个条形图上画平均线。

我的解决方案:

在数据集中,我添加类型为'line'的元素:

https://stackblitz.com/edit/ng2-charts-bar-template?file=src%2Fapp%2Fapp.component.ts

public barChartLabels: Label[] = ['2006', '2007', '2008', '2009', '2010', '2011', '2012'];
public barChartType: ChartType = 'bar';
public barChartLegend = true;
public barChartPlugins = [];

public barChartData: ChartDataSets[] = 
[
{ data: [65, 59, 80, 81, 56, 55, 40], label: 'Series A' },
{ data: [28, 48, 40, 19, 86, 27, 90], label: 'Series B' },
**{ data: [48, 48, 48, 48, 48, 48, 48], label: 'Series C', type: '*line*' }**
];

这条线不是从 y 轴的起点开始的,它没有连接到那个 y 轴。 (我理解,因为它在 bar 图表中)

但是我仍然希望它显示如下,它将从 y 轴的开头开始:

这是我想要的结果:

【问题讨论】:

    标签: angular bar-chart linechart ng2-charts chartjs-2.6.0


    【解决方案1】:

    我找到了一个解决方案,在该解决方案中,您必须在结尾和开头放置虚拟值,然后再进行操作。

    在要显示的每个数据集的结尾和开头设置0 很重要,还记得在结尾和开头为标签设置占位符文本。

    组件

    public chart = {
      "datasets": [
        { "data": [0, 30, 20, 40, 35, 45, 33, 0, 0], "label": "Bar 1" },
        { "data": [0, 50, 60, 55, 59, 30, 40, 0, 0], "label": "Bar 2" },
        { "data": [45, 45, 45, 45, 45, 45, 45, 45, 45], "label": "Line", "type": "line" }
      ],
      "labels": ["FirstPlaceholder", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday", "LastPlaceholder"],
      "options": {
        "legend": {
          "text": "You awesome chart with average line",
          "display": true,
        },
        "scales": {
          "yAxes": [{
            "ticks": {
            "beginAtZero": true
            }
          }],
          "xAxes": [{
            "ticks": {
            "min": "Monday",
            "max": "Sunday",
            }
          }],
        }
      }
    };
    

    模板

    <canvas baseChart
            chartType="bar"
            [datasets]="chart.datasets"
            [labels]="chart.labels"
            [options]="chart.options"
            legend="true">
    </canvas>
    

    她是Stackblitz
    这就是它的外观。

    【讨论】:

    • 我解决了这个问题。非常感谢你的帮助。祝你有美好的一天。
    • @Norbert Bartko 。你能告诉我如何在这里应用更改事件吗?就像会有一个最近 7 天的下拉列表,过去 7 年的最后 7 个月。根据用户选择如何渲染/重新填充数据集
    猜你喜欢
    • 2021-04-25
    • 2017-06-21
    • 1970-01-01
    • 2019-04-02
    • 2020-12-10
    • 2017-09-26
    • 1970-01-01
    • 1970-01-01
    • 2012-10-16
    相关资源
    最近更新 更多