我的目标是在运行时添加一个 QML ChartView
和可变数量的 LineSeries
。在用户选择并加载包含数据的文件之前,不知道需要添加多少 LineSeries
。
我试图在 LineSeries
中创建所有 Repeater
,但没有运气。我怀疑这是因为 ChartView
不知道如何处理一堆 Item
。由于 Repeater
不适用于 LineSeries
,因此不可能让 Repeater
直接创建 QObject
:
Repeater {
model: numberOfColumnsInModel / 2
delegate: Item {
LineSeries {
id: lineSeries
axisX: xAxis
axisY: yAxis
VXYModelMapper {
id: modelMapper
model: lineChart.model //Reimplemented QAbstractTableModel
xColumn: index * 2
yColumn: index * 2 + 1
}
onHovered: {
console.log("Do something...");
}
}
}
}
在我在网上看到的示例中,每个
LineSeries
都是硬编码的——ChartView
中的每一行一次——对我没有用。 最佳答案
使用强制文档,卢克。
在下面的示例中,在启动时创建具有随机点数的随机线数:
import QtQuick 2.7
import QtQuick.Window 2.2
import QtQuick.Controls 2.0
import QtCharts 2.1
Window {
id: window1
title: "Chart test"
visible: true
width: 600
height: 400
ChartView {
id: chart
anchors.fill: parent
axes: [
ValueAxis{
id: xAxis
min: 1.0
max: 10.0
},
ValueAxis{
id: yAxis
min: 0.0
max: 10.0
}
]
Component.onCompleted: {
var seriesCount = Math.round(Math.random()* 10);
for(var i = 0;i < seriesCount;i ++)
{
var series = chart.createSeries(ChartView.SeriesTypeLine, "line"+ i, xAxis, yAxis);
series.pointsVisible = true;
series.color = Qt.rgba(Math.random(),Math.random(),Math.random(),1);
series.hovered.connect(function(point, state){ console.log(point); }); // connect onHovered signal to a function
var pointsCount = Math.round(Math.random()* 20);
var x = 0.0;
for(var j = 0;j < pointsCount;j ++)
{
x += (Math.random() * 2.0);
var y = (Math.random() * 10.0);
series.append(x, y);
}
}
}
}
}
关于qt - QML,在运行时创建 LineSeries,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47928276/