amcharts不能以正确的格式表示数据

时间:2019-12-21 17:19:04

标签: javascript html json charts amcharts

我正在从数据库中检索值并将其推入amchart(XYChart),其中x轴的值无法正常工作。

这是图表代码

var chart5;
  function generateChart5(json) {
    chart5 = am4core.create("chart5-data", am4charts.XYChart);
    chart5.data = json;
    var dateAxis = chart5.xAxes.push(new am4charts.DateAxis());
    dateAxis.renderer.minGridDistance = 50;
    function createAxisAndSeries(field, name, opposite, bullet) {
      var valueAxis = chart5.yAxes.push(new am4charts.ValueAxis());
      var series = chart5.series.push(new am4charts.LineSeries());
      series.dataFields.valueY = field;
      series.dataFields.dateX = "time";
      series.strokeWidth = 2;
      series.yAxis = valueAxis;
      series.name = name;
      series.tooltipText = "{name}: [bold]{valueY}[/]";
      series.tensionX = 0.8;
      var interfaceColors = new am4core.InterfaceColorSet();
      valueAxis.renderer.line.strokeOpacity = 1;
      valueAxis.renderer.line.strokeWidth = 2;
      valueAxis.renderer.line.stroke = series.stroke;
      valueAxis.renderer.labels.template.fill = series.stroke;
      valueAxis.renderer.opposite = opposite;
      valueAxis.renderer.grid.template.disabled = true;
      valueAxis.renderer.labels.template.disabled = true; //disables labels
    }

    createAxisAndSeries("angry", "Angry", true, "circle");
    createAxisAndSeries("disgust", "Disgust", true, "triangle");
    createAxisAndSeries("happy", "Happy", true, "rectangle");
    createAxisAndSeries("neutral", "Neutral", true, "circle");
    createAxisAndSeries("sad", "Sad", true, "triangle");
    createAxisAndSeries("surprise", "Surprise", true, "rectangle");
    createAxisAndSeries("fear", "Fear", true, "rectangle");

    chart5.legend = new am4charts.Legend();

    chart5.cursor = new am4charts.XYCursor();
  };

这就是json文件的样子

[{
  "time": 1,
  "angry": 141,
  "disgust": 118,
  "happy": 106,
  "neutral": 117,
  "sad": 138,
  "surprise": 132,
  "fear": 135
},
.
.
.
.]

这就是图表的样子

amcharts

1 个答案:

答案 0 :(得分:0)

Okey,我不知道amcharts有不同类型的Axes。原来这是两行。

var dateAxis = chart5.xAxes.push(new am4charts.DateAxis());
      series.dataFields.dateX = "time";

我将它们更改为此:

var dateAxis = chart5.xAxes.push(new am4charts.valueAxis());
      series.dataFields.valueX = "time";

感谢xorspark