如何实现amchart堆积列图表可滚动

时间:2019-01-16 13:24:14

标签: javascript angular amcharts

我实现了下面的堆叠柱形图:

enter image description here

如您所见,某些栏未显示标签名称。

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

enter image description here

公共汽车,现在看不到所有物品。我认为我需要添加一个滚动条。 请你帮助我好吗?基本上,我想在此图表上添加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>

1 个答案:

答案 0 :(得分:0)

我找到了一个解决方案。请按照以下步骤操作:

// 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();

}

按照下面的打印显示如何获得

enter image description here