简体   繁体   English

如何实现 amchart 堆积柱形图可滚动

[英]How to implement amchart stacked columns chart scrollable

I had implemented the follow stacked column chart below:我已经实现了下面的堆积柱形图:

在此处输入图片说明

As you can see, some bar does not appear the label name.如您所见,有些栏没有出现标签名称。

I tried increase the width from this chart and now I can see the label from bars (look the new print below)我尝试增加此图表的宽度,现在我可以从条形图中看到标签(看看下面的新打印)

在此处输入图片说明

Bus now I cannot see all items.巴士现在我看不到所有项目。 i think that I need add a scroll bar.我想我需要添加一个滚动条。 Could you please help me?请你帮助我好吗? Basically I would like to add a X-axis scroll at this chart.基本上我想在这个图表上添加一个 X 轴滚动。

Note: I'm using the amchart version 4.注意:我使用的是 amchart 版本 4。

I'm using this example: https://www.amcharts.com/demos/stacked-column-chart/我正在使用这个例子: https : //www.amcharts.com/demos/stacked-column-chart/

follow my code below.按照我下面的代码。

method used to build the 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 code 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>

I found one solution for it.我找到了一种解决方案。 follow below:遵循以下:

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

So my full method that build chart is:所以我构建图表的完整方法是:

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

}

Follow the print below showing how it got按照下面的打印显示它是如何得到的

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM