javascript Amcharts 5 -平滑折线图-双折线图

s1ag04yj  于 2022-12-28  发布在  Java
关注(0)|答案(1)|浏览(288)

我正在处理这个问题。我已经从amcharts v5平滑了折线图。
并且一切都工作正常,直到我想要添加第二行到图表。它只显示一个折线图。而且我需要添加第二行到同一个图表。
如果我将数据值更改为另一个图表。它工作正常,但同样的问题,只有一行。
所以我的问题是,如何在同一个图表中添加第二行。

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

以下是codepen的工作解决方案:https://codepen.io/tom-august/pen/KKQLgbm
多谢了。

cbeh67ev

cbeh67ev1#

在您的数据中,应该有几个值字段,如下所示:

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
  }
];

然后,您需要为要显示的每一行创建一个系列,并将每个系列与相应的值字段相关联。您可以使用以下函数来完成此操作:

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

一个一个二个一个一个一个三个一个一个一个一个一个四个一个

相关问题