【问题标题】:How to implement amchart stacked columns chart scrollable如何实现amchart堆积柱形图可滚动
【发布时间】:2019-01-16 13:24:13
【问题描述】:

我已经实现了下面的堆积柱形图:

如您所见,有些栏没有出现标签名称。

我尝试增加这张图表的宽度,现在我可以从条形图中看到标签(看看下面的新打印)

Bus 现在我看不到所有项目。我认为我需要添加一个滚动条。 请你帮助我好吗?基本上我想在这个图表上添加一个 X 轴滚动。

注意: 我正在使用 amchart 版本 4。

我正在使用这个例子:https://www.amcharts.com/demos/stacked-column-chart/

按照我下面的代码。

用于构建图表的方法

private buildChart(dataChart) {

  /* Chart code */
  // Themes begin
  am4core.useTheme(am4themes_animated);
  // Create chart instance
  const chart = am4core.create('chartdiv', am4charts.XYChart);

  for (const data of dataChart) {
    chart.data.push(data);
  }

  console.log(chart);
  // Create axes
  const categoryAxis = chart.xAxes.push(new am4charts.CategoryAxis());
  categoryAxis.dataFields.category = 'model';
  categoryAxis.renderer.grid.template.location = 0;

  const valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
  valueAxis.renderer.inside = true;
  valueAxis.renderer.labels.template.disabled = true;
  valueAxis.min = 0;

  // Create series
  function createSeries(field, name) {

    // Set up series
    const series = chart.series.push(new am4charts.ColumnSeries());
    series.name = name;
    series.dataFields.valueY = field;
    series.dataFields.categoryX = 'model';
    series.sequencedInterpolation = true;

    // Make it stacked
    series.stacked = true;

    // Configure columns
    series.columns.template.width = am4core.percent(60);
    series.columns.template.tooltipText = '[bold]{name}[/]\n[font-size:14px]{categoryX}: {valueY}';

    // Add label
    const labelBullet = series.bullets.push(new am4charts.LabelBullet());
    labelBullet.label.text = '{valueY}';
    labelBullet.locationY = 0.5;

    return series;
  }

  createSeries('DISCONNECTED', 'DISCONNECTED');
  createSeries('AVAILABLE', 'AVAILABLE');

  // Legend
  chart.legend = new am4charts.Legend();

}

html代码

<div class="row">
  <div class="col-md-12 teste">
    <app-card [title]="'Available Devices'" >
      <div id="chartdiv" [style.height]="'400px'" [style.width]="'4000px'"></div>
    </app-card>
  </div>

【问题讨论】:

    标签: javascript angular amcharts


    【解决方案1】:

    我找到了一种解决方案。如下:

    // I use the scrollbarX to create a horizontal scrollbar 
    chart.scrollbarX = new am4core.Scrollbar();
    
    // here I set the scroolbar bottom the chart
    chart.scrollbarX.parent = chart.bottomAxesContainer;
    
    //here I chose not to show the startGrip (or button that expand the series from chart)
    chart.scrollbarX.startGrip.hide();
    chart.scrollbarX.endGrip.hide();
    
    // here I set the start and end scroolbarX series that I would like show in chart initially
    chart.scrollbarX.start = 0;
    chart.scrollbarX.end = 0.25;
    
    // here I chose not to show the zoomOutButton  that appear above from chart
    chart.zoomOutButton = new am4core.ZoomOutButton();
    chart.zoomOutButton.hide();
    

    所以我构建图表的完整方法是:

    private buildChart(dataChart) {
    
      /* Chart code */
      // Themes begin
      am4core.useTheme(am4themes_animated);
      // Create chart instance
      const chart = am4core.create('chartdiv', am4charts.XYChart);
    
      for (const data of dataChart) {
        chart.data.push(data);
      }
    
      // Create axes
      const categoryAxis = chart.xAxes.push(new am4charts.CategoryAxis());
      categoryAxis.dataFields.category = 'model';
      categoryAxis.renderer.grid.template.location = 0;
    
      const valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
      valueAxis.renderer.inside = true;
      valueAxis.renderer.labels.template.disabled = true;
      valueAxis.min = 0;
    
      // Create series
      function createSeries(field, name) {
    
        // Set up series
        const series = chart.series.push(new am4charts.ColumnSeries());
        series.name = name;
        series.dataFields.valueY = field;
        series.dataFields.categoryX = 'model';
        series.sequencedInterpolation = true;
    
        // Make it stacked
        series.stacked = true;
    
        // Configure columns
        series.columns.template.width = am4core.percent(60);
        series.columns.template.tooltipText = '[bold]{name}[/]\n[font-size:15px]{categoryX}: {valueY}';
    
        // Add label
        const labelBullet = series.bullets.push(new am4charts.LabelBullet());
        labelBullet.label.text = '{valueY}';
        labelBullet.locationY = 0.5;
    
        return series;
      }
    
      createSeries('DISCONNECTED', 'DISCONNECTED');
      createSeries('AVAILABLE', 'AVAILABLE');
    
      // Legend
      chart.legend = new am4charts.Legend();
      chart.scrollbarX = new am4core.Scrollbar();
      chart.scrollbarX.parent = chart.bottomAxesContainer;
      chart.scrollbarX.startGrip.hide();
      chart.scrollbarX.endGrip.hide();
      chart.scrollbarX.start = 0;
      chart.scrollbarX.end = 0.25;
    
      chart.zoomOutButton = new am4core.ZoomOutButton();
      chart.zoomOutButton.hide();
    
    }
    

    按照下面的打印显示它是如何得到的

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-02
      相关资源
      最近更新 更多