Search code examples
javascripthtmljsonchartsamcharts

amcharts doesn't represent data in right format


I'm retrieving value from database and pushing it to amchart (XYChart) the value of x Axes not working correctly.

this is the chart code

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

And this is how the json file looks like

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

and this is how the charts looks like

amcharts


Solution

  • Okey I didn't know that amcharts has different types of Axes. it turns out that these 2 lines are the problem.

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

    I changed them to this:

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

    Geat thanks for xorspark