如何在 QML 中创建图表?

Syl*_*eau 6 application-development qml ubuntu-sdk ubuntu-touch

我想在我的应用程序中添加一个页面,该页面不仅包含原始数据,还包含用于显示数据的直观图表。

是否有本机组件或第三方插件可以用来在 QML 中创建此类元素?

我正在寻找一种开源解决方案,理想情况下,它不仅可以在 ubuntu-touch 上运行,还可以在桌面系统上运行。

Syl*_*eau 13

您可以使用QChart.js - QML绑定Charts.js(使用canvas元素简单的HTML5图表JavaScript库)

在此处输入图片说明

我在这里分叉了该项目以支持调整大小事件(用于桌面使用)。我基本上需要重置画布上下文以允许调整大小事件以更新的窗口大小正确重绘表面(参见http://qt-project.org/forums/viewthread/37313

QML 示例:

在此处输入图片说明

以下代码段创建了上面的饼图页面:

import QtQuick 2.0
import QtQuick.Layouts 1.1
import Ubuntu.Components 0.1
import "."
import "QChart.js" as Charts

MainView {
    id: root
    width: units.gu(50)
    height: units.gu(80)

    PageStack {
        id: pageStack
        Component.onCompleted: push(page0)

        Page {
            id: page0
            title: i18n.tr("Test Results")

            ColumnLayout {
                spacing: units.gu(2)
                anchors.margins: units.gu(2);
                anchors.fill: parent

                Label {
                    fontSize: "x-large"
                    text: "Summary"
                }

                Chart {
                    id: chart_pie;
                    Layout.fillHeight: true
                    Layout.fillWidth: true
                    chartAnimated: true;
                    chartAnimationEasing: Easing.Linear;
                    chartAnimationDuration: 1000;
                    chartType: Charts.ChartType.PIE;
                    chartOptions: {"segmentStrokeColor": "#ECECEC"};
                    chartData: [
                        {value: 15, color: "#6AA84F"},
                        {value:  3, color: "#DC3912"},
                        {value:  5, color: "#FF9900"}];
                }

                Column {
                    id: legend
                    Row {
                        spacing: units.gu(1)
                        Text {
                            text: "?"
                            color:"#6AA84F"
                        }
                        Text {
                            text: "15 tests passed"
                        }
                    }
                    Row {
                        spacing: units.gu(1)
                        Text {
                            text: "?"
                            color:"#DC3912"
                        }
                        Text {
                            text: "3 tests failed"
                        }
                    }
                    Row {
                        spacing: units.gu(1)
                        Text {
                            text: "?"
                            color:"#FF9900"
                        }
                        Text {
                            text: "5 tests skipped"
                        }
                    }
                }

                Button {
                    id: button
                    Layout.fillWidth: true
                    color: "#009E0F";
                    text: "Save detailed report";
                    font.bold: true;
                    onClicked: {
                        button.color = "#009E0F"
                        chart_pie.repaint();
                    }
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)