QML ChartView with DateTimeAxis not able to plot seconds - qt

I am trying to show ChartView, with X axis as hh:mm so I am using DateTimeAxis.
As per the DateTimeAxis we need to provide min and max as JavaScript Date() object.
I want to have the hh:mm to range between 00:00 to 05:00 so I am using below code.
ChartView {
ValueAxis {
id: chartYAxis
min: 0
max: 400
color: "#8c8a89"
labelsColor: "#9c9998"
labelFormat: "%d"
tickCount: 5
titleText: "Y Axis"
gridLineColor: "gray"
titleFont.pointSize: 10
}
DateTimeAxis {
id: chartXAxis
min: new Date(1970, 1, 1, 0, 0, 0, 0) // 00:00
max: new Date(1970, 1, 1, 5, 0, 0, 0) // 05:00
format: "hh:mm"
tickCount: 6
labelsColor: "white"
gridVisible: false
lineVisible: true
titleText: "Time (hh:mm)"
titleFont.pointSize: 10
}
SplineSeries {
id: spline1
name: "line1"
color: "white"
width: 2
axisX: chartXAxis
axisY: chartYAxis
}
}
But the time for X axis that I am receiving is in seconds, not epoch seconds.
For example I receive X axis time in seconds as: [400, 5000, 60, 9000, 10000]
I am not sure how to plot these on the X axis I tried to append to spline1 as X, Y but no luck.
When I plot these seconds on the Chart then I don't see the line graph.
Do I need to convert seconds to epoch?

Related

Confused with PathView?

I am trying to know how PathView works. I follow the official doc to write some sample code, the code work well but the final result is not what I wanted?
The Code
import QtQuick
Rectangle {
id: root
border.color: "blue"
Component {
id: theDele
Rectangle {
id: rect
color: Qt.rgba(Math.random(), Math.random(),Math.random(), 1)
visible: PathView.onPath
width: 50; height: 50
scale: PathView.itemScale
rotation: PathView.itemAngle
property var rotX: PathView.itemAngle
transform: [
Translate {x: 25; y: 25}
]
Text {
anchors.centerIn: parent
text: index + "ABC"
}
}
}
PathView {
anchors.fill: parent
model: 100
pathItemCount: 10
delegate: theDele
path: Path {
startX: 0; startY: 100
PathAttribute {name: "itemAngle"; value: -90}
PathAttribute {name: "itemScale"; value: 0.5}
PathLine {x: 300; y: 100}
PathPercent {value: 0.2}
PathAttribute {name: "itemAngle"; value: 0}
PathAttribute {name: "itemScale"; value: 1}
PathLine {x: 300; y: 500}
PathPercent {value: 0.8}
PathAttribute {name: "itemAngle"; value: 90}
PathAttribute {name: "itemScale"; value: 0.5}
PathLine {x: 600; y: 500}
PathPercent { value: 1 }
}
}
}
I want work like this
In this demo, I try to draw three PathLine; 1st Line proportion is
0.2; 2nd Line proportion is 0.8;3rd Line proportion is 0.2.
So 1st Line should have 2 Items; 2nd Line should have 6 Items, and 3rd Line should have 2 Items. But This demo 2nd Line has 7 Items?
And 1st Line of all Items should scale 0.5, and rotate -90, And 2nd Line of all Items should scale 1, and rotate 0, And 3rd Line of all Items should scale 0.5, and rotate 90, But the demo items seem to scale and rotate linearly ?

How to add text in plotting area of chartview in qml

How do I place text at specific (x,y) locations within the plotting area of a QML ChartView type?
For example, I would like to place text at the location XYPoint{x: -3; Y: 20}
I don't want to place at window's(x,y), i want to put at plotting area's (x,y)
I read documentation,but i don't find any property !!!!!!
//ChartView for plotting points
ChartView{
id:chrt
anchors.fill: parent
height: parent.height
width: parent.width
legend.visible: false
backgroundColor: "black"
//X- axis
ValueAxis{
id: x_axis
min: -5
max: 0
tickCount: 6
}
//Right Y axis
ValueAxis{
id:right_y_axis
min:0
max:40
tickCount: 5
}
//Left Y axis
ValueAxis{
id:left_y_axis
min:0
max:40
tickCount: 5
}
//Line series for wave 1
LineSeries{
id:l1
axisY: left_y_axis
axisX:x_axis
color: "yellow"
width: 1
}
//Line series for wave 2
LineSeries{
id:l2
axisYRight: right_y_axis
style: Qt.DashLine
color: "yellow"
width: 0.6
}
}
Ok, you can use ChartView.mapToPosition to calculate the global position of the specified point from a series (or some another point inside the series), for example:
ChartView{
id:chart
anchors.fill: parent
backgroundColor: "black"
LineSeries{
id: series
XYPoint { x: 10; y: 5 }
XYPoint { x: 10; y: 1 }
XYPoint { x: 15; y: 5 }
XYPoint { x: 20; y: 10 }
XYPoint { x: 25; y: 5 }
XYPoint { x: 30; y: 20 }
XYPoint { x: 40; y: 10 }
}
Text {
id: txt
text: "Hello"
color: "white"
}
onWidthChanged: updatePointPosition();
onHeightChanged: updatePointPosition();
function updatePointPosition()
{
var p = chart.mapToPosition(series.at(3), series);
txt.x = p.x;
txt.y = p.y;
}
}

QML-How to change color of one point in chartview?

How to change color of one particular point in chartview in qml.i.e now all x axis values are displaying in black color but i want odd number to be displayed in red color.I want as shown in image where label colors in y axis are of different colors.
here is my piece of code
ChartView {
id:chartView
width: 2*horizontalList.width
height:horizontalList.height
antialiasing: true
animationOptions: ChartView.SeriesAnimations
legend.visible:false
ValueAxis {
id: scaleAxisX
min: 0
max: pointsX.length
tickCount: pointsX.length+1
labelFormat: "%.0f"
titleVisible: false
gridVisible: true
}
ValueAxis {
id: scaleAxisY
min: 0
max: 1.0
tickCount: 6
//labelFormat: "%.0f"
titleVisible: false
gridVisible: false
labelsVisible: false
labelsColor: "#757575"
}
ScatterSeries {
id: scatterSeries
axisXTop: scaleAxisX
axisY: scaleAxisY
color: "black"
markerSize: 12
}
}
As shown in second image ,the top line with red circle is a X axis(valueAxis) of chartview.i want to make the number inside the red circle to be in red color.
You can add a different ScatterSeries with color value;
import QtQuick 2.0
import QtCharts 2.0
ChartView {
title: "Scatters"
anchors.fill: parent
antialiasing: true
ScatterSeries {
color: "black"
id: scatter1
name: "Scatter1"
XYPoint { x: 1; y: 1 }
XYPoint { x: 2; y: 2 }
XYPoint { x: 3; y: 3 }
XYPoint { x: 4; y: 4 }
}
ScatterSeries {
color: "red"
name: "Scatter2"
XYPoint { x: 1.5; y: 1.5 }
}
}

Customizing BarSet in QML Charts

I am trying to change the look of a BarChart using QtCharts 2.1. At the moment I apply the desired color and styling to the single QBarSets like this:
import QtQuick 2.8
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0
import QtCharts 2.1
import QtQuick.Window 2.2
ChartView {
id: root
width: 600
height: 400
antialiasing: true
backgroundColor: "transparent"
legend.visible: false
property var histogramProvider
Connections {
target: histogramProvider
onHistogramChanged: {
console.log("histogram changed")
histogramProvider.updateSeries(histogramSeries)
console.log(histogramSeries)
console.log(histogramSeries.at(0))
histogramSeries.at(0).color = Qt.rgba(0.9, 0.2, 0.1, 0.5)
}
onAxesChanged: {
xAxis.min = xMin
xAxis.max = xMax
yAxis.min = yMin
yAxis.max = yMax
}
}
ValueAxis {
id: yAxis
min: 0
max: 100
tickCount: 3
gridVisible: false
labelsColor: Style.fontColorLight
labelsFont: Qt.font({pointSize: 8})
titleText: qsTr("Count")
labelFormat: "%.1f"
}
ValueAxis {
id: xAxis
tickCount: 3
gridVisible: false
labelsColor: "white"
labelFormat: "%i"
min: 0
max: 100
titleText: qsTr("Elongation")
}
StackedBarSeries {
id: histogramSeries
axisX: xAxis
axisY: yAxis
}
}
I can see the BarSets with the default colors in the chart, hwever I can not change the colors. The code produces the following console output from onHistogramChanged:
qml: QtCharts::DeclarativeStackedBarSeries(0x1df550cd830)
qml: null
qrc:/qml/Histogram.qml:27: TypeError: Type error
I update the BarSeries from C++ like this:
void HistogramProvider::updateSeries(QStackedBarSeries *barSeries)
{
[...]
barSeries->clear();
QBarSet* set = new QBarSet("Histogram", barSeries);
set->setBorderColor(QColor("transparent"));
set->setColor(QColor("#6f74dd"));
for(int i = 0; i < bins.size(); i++) {
set->append(bins.at(i));
}
qDebug() << "blubb";
barSeries->append(set);
}
What am I doing wrong here?

Customize Bar-series and BarCategoryAxis in QML

I need to customize the BarCategoryAxis labels and BarSeries. Following is the code:
import QtQuick 2.6
import QtQuick.Window 2.2
import QtQuick.Controls 2.0
import QtCharts 2.2
ApplicationWindow {
id: window
width: 640
height: 480
visible: true
ChartView {
id: chartview
width: parent.width
height: 300
BarSeries {
name: "BarSeries"
axisX: BarCategoryAxis {
id:barAxis
property int minCategory: 10
property int maxCategory: 100
property int category: 50
categories: ["10","20","30","40","50","60","70","80","90","100"]
min: Math.floor(((minCategory + sb.position *
(maxCategory - minCategory - category)) + 9)/10)*10
max: Math.floor((((minCategory + sb.position *
(maxCategory - minCategory - category)) + category) + 9)/10) *10
}
axisY: ValueAxis {
id:axisY
min: 0
max: 70
tickCount: 5
labelFormat: "%.0f"
}
BarSet { values: [2, 2, 3, 4, 5, 6,7] }
BarSet { values: [5, 1, 2, 4, 1, 7] }
BarSet { values: [3, 5, 8, 13, 5, 8] }
}
}
Slider {
id: sb
anchors {
bottom: parent.bottom
left: parent.left
right: parent.right
}
height: 30
}
}
1) How to align the X Axis labels exactly below the grid? currently, it is at center as below:
I Tried with ValueAxis but it will not plot the bars properly but labels will be exactly below the grid.
2) Currently, slider moves the graph but not fluent. Can anyone suggest a better approach?
1) Did you try with CategoryAxis instead of BarCategoryAxis, it has an attribute labelsPosition.
2) First you should make your computation inside the Slider slot :
Slider {
onPositionChanged: {
barAxis.min = ...
barAxis.max = ...
}
}
Then I suggest to use ChartView functions like scrollRight(...), it's much more smooth.

Resources