【问题标题】:Angular Primeng Charts: Bar Chart with Line ChartAngular Primeng 图表:带折线图的条形图
【发布时间】:2019-08-02 20:00:29
【问题描述】:

请帮忙。这里有没有人已经在angular2+中实现了这种类型的图表组合?我目前使用 Angular 6。

http://www.chartjs.org/samples/latest/charts/combo-bar-line.html

请分享您的解决方案。谢谢。

【问题讨论】:

    标签: angular bar-chart primeng


    【解决方案1】:
     <p-chart type="bar" [data]=monthlyConfig></p-chart>
    

    并且您需要将类型参数传递给配置。

        this.monthlyConfig = {
            labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
            datasets: [
        {
          type: 'line',
          label: 'Dataset 1',
          borderColor: '#FF671F',
          borderWidth: 2,
          fill: false,
          data: Array.from({ length: 6 }, () => Math.floor(Math.random() * 100))
        }, 
        {
          type: 'bar',
          label: 'Dataset 2',
          backgroundColor: '#FF671F',
          data: Array.from({ length: 6 }, () => Math.floor(Math.random() * 100)),
          borderColor: 'white',
          borderWidth: 2
        },
      ]
    }
    

    【讨论】:

      【解决方案2】:

      使用以下角度图表https://canvasjs.com/angular-charts/

      app.component.html

      <div id="chartContainer" style="height: 370px; width: 100%; margin-left:auto;margin-right:auto;"></div>
      

      app.component.ts

       import { Component, OnInit } from '@angular/core';
          import * as CanvasJS from './CanvasJS';
      
      @Component({
          selector: 'app-root',
          templateUrl: './app.component.html'
      })     
      export class AppComponent implements OnInit {
          ngOnInit() {
          let chart = new CanvasJS.Chart("chartContainer", {
              theme: "light2",
              animationEnabled: true,
              exportEnabled: true,
              title:{
                  text: "Monthly Expense"
              },
              data: [{
                  type: "pie",
                  showInLegend: true,
                  toolTipContent: "<b>{name}</b>: ${y} (#percent%)",
                  indexLabel: "{name} - #percent%",
                  dataPoints: [
                      { y: 450, name: "Food" },
                      { y: 120, name: "Insurance" },
                      { y: 300, name: "Traveling" },
                      { y: 800, name: "Housing" },
                      { y: 150, name: "Education" },
                      { y: 150, name: "Shopping"},
                      { y: 250, name: "Others" }
                  ]
              }]
          });
      
          chart.render();
          }
      }
      

      【讨论】:

      • 感谢您的回复,但我能够从另一篇帖子*.com/questions/37571243/…获得有关 ff 答案的帮助,chartjs 已经通过添加另一个数据点并指定类型“line: on”来支持带有折线图的条形图附加折线图数据集。
      • 我可以在同一行有两个图表吗?