wxPython 其他创建饼图的方法

wxPython other way to create Pie Chart

已编辑

我有一个代码可以在 wxPython WebView 中显示 html 但它只是加载 html 而没有 css 和 javascript 在 html 中文件。这是我的代码。

gui.py

class MainFrame(wx.Frame):
    def __init__( self, parent ):
        wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = u"PlagDetect", pos = wx.DefaultPosition, size = wx.Size( 493,389 ),
        self.htmlSummary = wx.html2.WebView.New(self)

        page = """
            <!DOCTYPE html>
            <html lang="en">
            <head>
                <meta charset="UTF-8">
                <title>Summary</title>
            </head>
            <body>
                <h1>Summary</h1>

                <div id="piechart"></div>

                <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>

                <script type="text/javascript">
                    // Load google charts
                    google.charts.load('current', {'packages':['corechart']});
                    google.charts.setOnLoadCallback(drawChart);

                    // Draw the chart and set the chart values
                    function drawChart() {
                      var data = google.visualization.arrayToDataTable([
                      ['Task', 'Hours per Day'],
                      ['Work', 8],
                      ['Eat', 2],
                      ['TV', 4],
                      ['Gym', 2],
                      ['Sleep', 8]
                    ]);

                      // Optional; add a title and set the width and height of the chart
                      var options = {'title':'My Average Day', 'width':550, 'height':400};

                      // Display the chart inside the <div> element with id="piechart"
                      var chart = new google.visualization.PieChart(document.getElementById('piechart'));
                      chart.draw(data, options);
                    }
                </script>
            </body>
            </html>
        """

        summary.htmlSummary.SetPage(page, "")

我找到了使用 PieCtrl 而不是使用 WebView 以其他方式创建饼图的答案,这要感谢 Saxony 的 @Rolf 先生。答案写在下面。

稍微清理一下您发布的代码后,它似乎运行良好。虽然,我没有看到任何 css,但 javascript 部分有效
注意:我 运行 在 Linux

import wx
import wx.html2


class MainFrame(wx.Frame):
    def __init__( self, parent ):
        wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = u"PlagDetect", pos = wx.DefaultPosition, size = wx.Size( 600,450 ))
        self.htmlSummary = wx.html2.WebView.New(self)
        page = """
            <!DOCTYPE html>
            <html lang="en">
            <head>
                <meta charset="UTF-8">
                <title>Summary</title>
            </head>
            <body>
                <h1>Summary</h1>

                <div id="piechart"></div>

                <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>

                <script type="text/javascript">
                    // Load google charts
                    google.charts.load('current', {'packages':['corechart']});
                    google.charts.setOnLoadCallback(drawChart);

                    // Draw the chart and set the chart values
                    function drawChart() {
                      var data = google.visualization.arrayToDataTable([
                      ['Task', 'Hours per Day'],
                      ['Work', 8],
                      ['Eat', 2],
                      ['TV', 4],
                      ['Gym', 2],
                      ['Sleep', 8]
                    ]);

                      // Optional; add a title and set the width and height of the chart
                      var options = {'title':'My Average Day', 'width':550, 'height':400};

                      // Display the chart inside the <div> element with id="piechart"
                      var chart = new google.visualization.PieChart(document.getElementById('piechart'));
                      chart.draw(data, options);
                    }
                </script>
            </body>
            </html>
        """

        self.htmlSummary.SetPage(page, "")

if __name__ == "__main__":

    app = wx.App()
    frame_1 = MainFrame(None)
    frame_1.Show()
    app.MainLoop()

回答您的评论,"is there any other way to create a pie chart in wxpython",是的,请参阅:https://wxpython.org/Phoenix/docs/html/wx.lib.agw.piectrl.PieCtrl.html

最简单的:

import wx
import wx.lib.agw.piectrl
from wx.lib.agw.piectrl import PieCtrl, PiePart

class Frame(wx.Frame):

    def __init__(self, parent):
        wx.Frame.__init__ (self, parent, -1, "Simple Pie Chart")

        panel = wx.Panel(self, -1, size=(650,650))
        # Create A Simple PieCtrl With 3 Sectors
        self._pie = PieCtrl(panel, -1, wx.DefaultPosition, wx.Size(180,270))

        self._pie.GetLegend().SetTransparent(True)
        self._pie.GetLegend().SetHorizontalBorder(10)
        self._pie.GetLegend().SetWindowStyle(wx.STATIC_BORDER)
        self._pie.GetLegend().SetLabelFont(wx.Font(10, wx.FONTFAMILY_DEFAULT,
                                                   wx.FONTSTYLE_NORMAL,
                                                   wx.FONTWEIGHT_NORMAL,
                                                   False, "Courier New"))
        self._pie.GetLegend().SetLabelColour(wx.Colour(0, 0, 127))

        self._pie.SetHeight(10)
        self._pie.SetAngle(0.35)

        part = PiePart()

        part.SetLabel("Label_1")
        part.SetValue(300)
        part.SetColour(wx.Colour(200, 50, 50))
        self._pie._series.append(part)

        part = PiePart()
        part.SetLabel("Label 2")
        part.SetValue(200)
        part.SetColour(wx.Colour(50, 200, 50))
        self._pie._series.append(part)

        part = PiePart()
        part.SetLabel("Label 3")
        part.SetValue(50)
        part.SetColour(wx.Colour(50, 50, 200))
        self._pie._series.append(part)
        self.Show()

app = wx.App()
frame = Frame(None)
app.MainLoop()