![](/img/trans.png)
[英]Get name of QML LineSeries when the LineSeries is hovered over - (LineSeries created dynamically)
[英]Get the name of a QML ChartView LineSeries when the LineSeries is clicked
我創建了一個程序,該程序從Internet讀取股票數據(時間序列)並將其顯示在QML ChartView中。 添加所需的所有線系列后,可以通過單擊按鈕將其刪除。
我想知道是否可以通過單擊線系列中的任意點來刪除線系列?
我正在像這樣動態添加系列:
// stockID is sent from C++ when the timeSeriesReady signal is emitted
var chartViewSeries = chartView.createSeries(ChartView.SeriesTypeLine, stockID, dateTimeAxis_chartView_xAxis, valueAxis_chartView_yAxis);
// Set chartViewSeries (AbstractSeries/LineSeries) properties
chartViewSeries.onClicked.connect(lineSeriesClicked);
chartViewSeries.onHovered.connect(lineSeriesHovered);
stockChart.setLineSeries(chartViewSeries);
我不想擁擠太多的帖子,所以我不會發布所有文件,但是:
lineSeriesClicked是此函數:
function lineSeriesClicked(lineSeriesPoint){ console.log(lineSeriesPoint); }
lineSeriesHovered是此函數:
function lineSeriesHovered(lineSeriesPoint, isHovered){ if(isHovered){ var date = new Date(lineSeriesPoint.x); console.log(date.getFullYear() + "-" + (((date.getMonth()+1) < 10) ? ("0" + (date.getMonth()+1).toString()) : (date.getMonth()+1)) + "-" + (((date.getDate()) < 10) ? ("0" + (date.getDate()).toString()) : (date.getDate())) + " -> " + lineSeriesPoint.y); } }
現在,在日志中,我看到了所有正確的數據,例如,懸停時:
qml: 2017-08-29 -> 64.91115963918442
點擊時:
qml: QPointF(1.50432e+12, 65.0453)
查看XYSeries QML類型( https://doc.qt.io/qt-5/qml-qtcharts-xyseries.html#clicked-signal ),我使用的單擊信號僅通過了一個點。
有什么方法可以獲取獲得點數據的線系列的名稱,以便刪除該系列? 也許通過某種上下文訪問或“ this”關鍵字?
非常感謝你!!!
我不知道您是否能解決這個問題,而只是為了文檔...我可以使用model-view-delegate解決它。 在我的示例中,我使用傳感器數據生成圖形,希望通過“系列”標簽連接不同的圖形(例如餅圖和線條)。 我可以使用這個model-view-delegate透視圖來做。 我是這樣想的:
import QtQuick 2.2
import QtQuick.Controls 2.3
import QtQuick.Window 2.10
import QtQuick.Layouts 1.3
import QtDataVisualization 1.2
import QtCharts 2.2
import Qt3D.Input 2.0
// I imported more than I used because it's a part of a big project...
Item{
id: mainWindow
ListModel{
id: jsonData
Component.onCompleted: // line
{
//create a request and tell it where the json that I want is
var req = new XMLHttpRequest();
var location = "URL-to-JSON-DATA"
//tell the request to go ahead and get the json
req.open("GET", location, true);
req.send(null);
//wait until the readyState is 4, which means the json is ready
req.onreadystatechange = function()
{
console.log("Req status:"+req.status+"("+req.readyState+")");
if (req.readyState == 4)
{
//turn the text in a javascript object while setting the ListView's model to it
if (req.responseText){
var result = JSON.parse(req.responseText);
for (var i =0; i < result.rows.length;i++){
// map the Json to a model
jsonData.append({"sensor":result['rows'][i]['value'][0],"activePower": result['rows'][i]['value'][1],"voltage":result['rows'][i]['value'][2], "current":result['rows'][i]['value'][3], "year":result['rows'][i]['key'][0],"month": result['rows'][i]['key'][1],"day":result['rows'][i]['key'][2], "hour":result['rows'][i]['key'][3], "minute":result['rows'][i]['key'][4] })
};
//Sucess string
console.log("JSON [Sensor,Date,Hour] loaded");
//Fail string :(
} else {console.log("Empty[Sensor,Date,Hour JSON");}
}
}
}
}
ChartView {
title: "Line"
id: mainChart
anchors.fill: parent
antialiasing: true
Repeater{
model: jsonData
Item {
Component.onCompleted: {
if( mainChart.series(sensor)) {
mainChart.series(sensor).append(10,activePower);
} else{
mainChart.createSeries(ChartView.SeriesTypeLine,sensor,lineXOrdinate,lineYOrdinate );
mainChart.series(sensor).append(hour+(minute*0.5)/30,activePower);
mainChart.series(sensor).hovered.connect(
function (point,state){
if (state){
console.log(">>>"+sensor); // print the Series label =D
}
});
}
if(activePower > lineYOrdinate.max){ // if the highest value is above the Y axis, reset it to value+10
lineYOrdinate.max = activePower +10;
}
}
}
}
}
ValueAxis {
id: lineYOrdinate
min:0
max:11
}
ValueAxis {
id: lineXOrdinate
min:0
max:24
}
}
X軸的長度為24個值,因為我要映射一天。 我希望這個幫助=)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.