简体   繁体   中英

Amcharts 5 - Smoothed line chart - two line charts

Im dealing with this problem. I have smoothed line chart from amcharts v5.

And everything is working properly until i want to add second line to the chart. It only displays one line chart. And i need to add second line to the same chart.

If I change data values to another chart. It is working fine but same problem, only one line.

So my question is, how to add second line into same chart.

am5.ready(function() {
// Create root element
// https://www.amcharts.com/docs/v5/getting-started/#Root_element
var root = am5.Root.new("chartdiv");

// Set themes
// https://www.amcharts.com/docs/v5/concepts/themes/
root.setThemes([
    am5themes_Animated.new(root)
]);

// Create chart
// https://www.amcharts.com/docs/v5/charts/xy-chart/
var chart = root.container.children.push(am5xy.XYChart.new(root, {
panX: true,
panY: true,
wheelX: "panX",    
wheelY: "zoomX",
pinchZoomX:true
}));
// Add cursor
// https://www.amcharts.com/docs/v5/charts/xy-chart/cursor/
var cursor = chart.set("cursor", am5xy.XYCursor.new(root, {
behavior: "none"
}));
cursor.lineY.set("visible", false);

function generateDataInvestice(roky,investice) {
   value = investice;
   return {
   date: roky,
   value: value
};
}

function generateDatasInvestice(count,i) {
  var data = [];
  var investice = 5;
  for (var i = 1; i < count; ++i) {
    data.push(generateDataInvestice(i,investice));
  }
  return data;
}

//generate uver
function generateDataUver(roky,uver) {
  value = uver;
  return {
    date: roky,
    value: value
  };
}
function generateDatasUver(count,i) {
  var data = [];
  var uver = 1;
  for (var i = 1; i < count; ++i) {
    data.push(generateDataUver(i,uver));
  }
  return data;
}
// Create axes
// https://www.amcharts.com/docs/v5/charts/xy-chart/axes/
var xAxis = chart.xAxes.push(am5xy.ValueAxis.new(root, {
  maxDeviation: 1,
  baseInterval: {
    count: 1
  },
  renderer: am5xy.AxisRendererX.new(root, {
  pan:"zoom"
}),
  tooltip: am5.Tooltip.new(root, {})
}));

var yAxis = chart.yAxes.push(am5xy.ValueAxis.new(root, {
  maxDeviation:1,
  renderer: am5xy.AxisRendererY.new(root, {
  pan:"zoom"
})
}));

// Add series
// https://www.amcharts.com/docs/v5/charts/xy-chart/series/
var series = chart.series.push(am5xy.SmoothedXLineSeries.new(root, {
  name: "Series",
  xAxis: xAxis,
  yAxis: yAxis,
  valueYField: "value",
  valueXField: "date",
  tooltip: am5.Tooltip.new(root, {
    labelText: "{valueY}"
  })
}));

series.fills.template.setAll({
  visible: true,
  fillOpacity: 0.2
});

series.bullets.push(function() {
  return am5.Bullet.new(root, {
    locationY: 0,
    sprite: am5.Circle.new(root, {
      radius: 4,
      stroke: root.interfaceColors.get("background"),
      strokeWidth: 2,
      fill: series.get("fill")
    })
  });
});

var data = generateDatasInvestice(30,0);
var data_uver = generateDatasUver(30,0);
series.data.setAll(data,data_uver);

series.appear(30);
chart.appear(1000, 100);

}); // end am5.ready()

Here is working soluiton on codepen: https://codepen.io/tom-august/pen/KKQLgbm

Thanks a lot.

In your data, you should have several value fields like so:

var data = [
  {
    date: new Date("2022-12-27").getTime(),
    value1: 10,
    value2: 20
  },
  {
    date: new Date("2022-12-28").getTime(),
    value1: 20,
    value2: 30
  },
  {
    date: new Date("2022-12-29").getTime(),
    value1: 10,
    value2: 20
  }
];

Then you need to create a series for each line that you want to display, associating each series with the corresponding value field. This can be done using a function:

function createSeries(name, field) {
  var series = chart.series.push(am5xy.SmoothedXLineSeries.new(root, {
    name: name,
    xAxis: xAxis,
    yAxis: yAxis,
    valueXField: "date",
    valueYField: field
  }));

  series.data.setAll(data);
}
  
createSeries("Series 1", "value1");
createSeries("Series 2", "value2");

 am5.ready(function() { var root = am5.Root.new("chartdiv"); var chart = root.container.children.push(am5xy.XYChart.new(root, {})); var data = [ { date: new Date("2022-12-27").getTime(), value1: 10, value2: 20 }, { date: new Date("2022-12-28").getTime(), value1: 20, value2: 30 }, { date: new Date("2022-12-29").getTime(), value1: 10, value2: 20 } ]; var xAxis = chart.xAxes.push(am5xy.DateAxis.new(root, { baseInterval: { timeUnit: "day", count: 1 }, renderer: am5xy.AxisRendererX.new(root, {}), })); var yAxis = chart.yAxes.push(am5xy.ValueAxis.new(root, { renderer: am5xy.AxisRendererY.new(root, {}) })); function createSeries(name, field) { var series = chart.series.push(am5xy.SmoothedXLineSeries.new(root, { name: name, xAxis: xAxis, yAxis: yAxis, valueXField: "date", valueYField: field })); series.data.setAll(data); } createSeries("Series 1", "value1"); createSeries("Series 2", "value2"); });
 #chartdiv { width: 100%; height: 350px; }
 <script src="https://cdn.amcharts.com/lib/5/index.js"></script> <script src="https://cdn.amcharts.com/lib/5/xy.js"></script> <div id="chartdiv"></div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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