Page 1 of 1
Using wxFreeChart in wxWidgets
Posted: Wed Aug 01, 2018 6:46 pm
by nona
Hello Everyone,
I am trying to draw a Pie Chart using wxFreeChart Lib here is the code I wrote, The problem is the charts is blank not showing anything. Anyone knows what is wrong with my code.
Thanks!
Code: Select all
// serie pieplot data
double data[] = {1.0, 2.0, 3.0} ;
wxString categories[] = {_("cat 1"), _("cat 2"), _("cat 3")};
wxColour colours[] = {wxColour(0x99, 0xCC, 0xFF), wxColour(0xFF, 0xFF, 0x99), wxColour(0x3D, 0xEB, 0x3D)} ;
ColorScheme* colorScheme = new ColorScheme(colours, WXSIZEOF(colours));
// first step: create plot
PiePlot *plot = new PiePlot();
// create dataset
CategorySimpleDataset* dataset = new CategorySimpleDataset(categories, WXSIZEOF(categories));
// and add serie to it
dataset->AddSerie(_("Serie 1"), data, WXSIZEOF(data));
// create category renderer for legend drawing
dataset->SetRenderer(new CategoryRenderer(*colorScheme));
// set color scheme
plot->SetColorScheme(colorScheme);
plot->SetDataset(dataset);
// set legend to plot
plot->SetLegend(new Legend(wxCENTER, wxRIGHT));
// Create a chart object with our plot.
Chart* chart = new Chart(plot, "My First Chart");
// Create a chart panel to display the chart.
wxChartPanel* chartPanel = new wxChartPanel(Panel1,
wxID_ANY, chart, wxPoint(0, 0), wxSize(1, 1));
BoxSizer1->Add(chartPanel, 1, wxGROW | wxEXPAND, 5);
BoxSizer1->Layout();
BoxSizer1->Fit(Panel1);
Re: Using wxFreeChart in wxWidgets
Posted: Sat Aug 04, 2018 1:32 am
by mill-j
I think your problem is how you declare your charts and add them to the frame. Your code works for the most part, try changing some things, like;
Code: Select all
wxBoxSizer* bSizer1;
bSizer1 = new wxBoxSizer( wxVERTICAL );
wxChartPanel* GraphChartPanel = new wxChartPanel(this,-1);
bSizer1->Add(GraphChartPanel, 1,wxALL|wxEXPAND, 5);
double data[] = {1.0, 2.0, 3.0} ;
wxString categories[] = {_("cat 1"), _("cat 2"), _("cat 3")};
wxColour colours[] = {wxColour(0x99, 0xCC, 0xFF), wxColour(0xFF, 0xFF, 0x99), wxColour(0x3D, 0xEB, 0x3D)} ;
ColorScheme* colorScheme = new ColorScheme(colours, WXSIZEOF(colours));
// first step: create plot
PiePlot *plot = new PiePlot();
// create dataset
CategorySimpleDataset* dataset = new CategorySimpleDataset(categories, WXSIZEOF(categories));
// and add serie to it
dataset->AddSerie(_("Serie 1"), data, WXSIZEOF(data));
// create category renderer for legend drawing
dataset->SetRenderer(new CategoryRenderer(*colorScheme));
// set color scheme
plot->SetColorScheme(colorScheme);
plot->SetDataset(dataset);
// set legend to plot
plot->SetLegend(new Legend(wxCENTER, wxRIGHT));
GraphChartPanel->SetChart(new Chart(plot, "My First Chart"));
this->SetSizer( bSizer1 );
this->Layout();
this->Centre( wxBOTH );
This works for me. It should give you somthing to work off of.
I hope this helps. In My experience the wxFreeChart demo is fairly hard to figure out when beginning.
Re: Using wxFreeChart in wxWidgets
Posted: Tue Aug 07, 2018 1:41 pm
by nona
Thanks for your reply I will test your code.
Re: Using wxFreeChart in wxWidgets
Posted: Tue Aug 07, 2018 1:51 pm
by nona
Thanks I got it worked now
Many thanks.
Re: Using wxFreeChart in wxWidgets
Posted: Thu Aug 09, 2018 3:22 pm
by nona
mill-j wrote:I think your problem is how you declare your charts and add them to the frame. Your code works for the most part, try changing some things, like;
Code: Select all
wxBoxSizer* bSizer1;
bSizer1 = new wxBoxSizer( wxVERTICAL );
wxChartPanel* GraphChartPanel = new wxChartPanel(this,-1);
bSizer1->Add(GraphChartPanel, 1,wxALL|wxEXPAND, 5);
double data[] = {1.0, 2.0, 3.0} ;
wxString categories[] = {_("cat 1"), _("cat 2"), _("cat 3")};
wxColour colours[] = {wxColour(0x99, 0xCC, 0xFF), wxColour(0xFF, 0xFF, 0x99), wxColour(0x3D, 0xEB, 0x3D)} ;
ColorScheme* colorScheme = new ColorScheme(colours, WXSIZEOF(colours));
// first step: create plot
PiePlot *plot = new PiePlot();
// create dataset
CategorySimpleDataset* dataset = new CategorySimpleDataset(categories, WXSIZEOF(categories));
// and add serie to it
dataset->AddSerie(_("Serie 1"), data, WXSIZEOF(data));
// create category renderer for legend drawing
dataset->SetRenderer(new CategoryRenderer(*colorScheme));
// set color scheme
plot->SetColorScheme(colorScheme);
plot->SetDataset(dataset);
// set legend to plot
plot->SetLegend(new Legend(wxCENTER, wxRIGHT));
GraphChartPanel->SetChart(new Chart(plot, "My First Chart"));
this->SetSizer( bSizer1 );
this->Layout();
this->Centre( wxBOTH );
This works for me. It should give you somthing to work off of.
I hope this helps. In My experience the wxFreeChart demo is fairly hard to figure out when beginning.
I tried to use the chart with FlexGridSizer but it did not work, did u try it before?
Re: Using wxFreeChart in wxWidgets
Posted: Thu Aug 09, 2018 5:20 pm
by mill-j
nona wrote:
I tried to use the chart with FlexGridSizer but it did not work, did u try it before?
Not yet. So far I've never needed to use it.
Re: Using wxFreeChart in wxWidgets
Posted: Thu Aug 09, 2018 6:02 pm
by doublemax
I tried to use the chart with FlexGridSizer but it did not work
"did not work" is not a useful error description. In any case, most likely it has nothing to do with wxFreeChart, only with your sizer code.
Re: Using wxFreeChart in wxWidgets
Posted: Fri Aug 10, 2018 2:34 pm
by nona
doublemax wrote:I tried to use the chart with FlexGridSizer but it did not work
"did not work" is not a useful error description. In any case, most likely it has nothing to do with wxFreeChart, only with your sizer code.
I used Frame-->wxFlexSizer-->wxPanel-->wxStaticBoxSizer
Re: Using wxFreeChart in wxWidgets
Posted: Fri Aug 10, 2018 2:35 pm
by nona
doublemax wrote:I tried to use the chart with FlexGridSizer but it did not work
"did not work" is not a useful error description. In any case, most likely it has nothing to do with wxFreeChart, only with your sizer code.
In this case I used: Frame-->Panel-->StaticBoxSizer
Re: Using wxFreeChart in wxWidgets
Posted: Fri Aug 10, 2018 2:38 pm
by doublemax
Please show the complete sizer code.
Re: Using wxFreeChart in wxWidgets
Posted: Fri Aug 10, 2018 2:40 pm
by nona
Code: Select all
#include "test.h"
#include <wx/pie/pieplot.h>
#include <wx/category/categorysimpledataset.h>
#include <wx/chart.h>
#include <wx/xy/XYPlot.h>
#include <wx/xy/xysimpledataset.h>
#include <wx/xy/xylinerenderer.h>
#include <wx/chartpanel.h>
#include <wx/aui/aui.h>
#include "wx/chartpanel.h"
#include <wx/treectrl.h>
#include "wx/wxfreechartdefs.h"
//(*InternalHeaders(test)
#include <wx/intl.h>
#include <wx/string.h>
//*)
//(*IdInit(test)
const long test::ID_PANEL1 = wxNewId();
const long test::ID_NOTEBOOK1 = wxNewId();
//*)
BEGIN_EVENT_TABLE(test,wxFrame)
//(*EventTable(test)
//*)
END_EVENT_TABLE()
test::test(wxWindow* parent,wxWindowID id,const wxPoint& pos,const wxSize& size)
{
//(*Initialize(test)
wxFlexGridSizer* FlexGridSizer1;
wxStaticBoxSizer* StaticBoxSizer1;
Create(parent, id, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE, wxT("id"));
SetClientSize(wxDefaultSize);
Move(wxDefaultPosition);
FlexGridSizer1 = new wxFlexGridSizer(0, 3, 0, 0);
Notebook1 = new wxNotebook(this, ID_NOTEBOOK1, wxDefaultPosition, wxDefaultSize, 0, wxT("ID_NOTEBOOK1"));
Panel1 = new wxPanel(Notebook1, ID_PANEL1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, wxT("ID_PANEL1"));
StaticBoxSizer1 = new wxStaticBoxSizer(wxHORIZONTAL, Panel1, _("Chart"));
Panel1->SetSizer(StaticBoxSizer1);
StaticBoxSizer1->Fit(Panel1);
StaticBoxSizer1->SetSizeHints(Panel1);
Notebook1->AddPage(Panel1, _("Page name"), false);
FlexGridSizer1->Add(Notebook1, 1, wxEXPAND, 8);
SetSizer(FlexGridSizer1);
FlexGridSizer1->Fit(this);
FlexGridSizer1->SetSizeHints(this);
//*)
wxChartPanel* GraphChartPanel = new wxChartPanel(Panel1,-1);
StaticBoxSizer1->Add(GraphChartPanel, 1,wxALL|wxEXPAND, 5);
double data[] = {1.0, 2.0, 3.0} ;
wxString categories[] = {_("Online Agents"), _("Offline Agents"), _("Warning")};
wxColour colours[] = {wxColour(0x4A46F7), wxColour(0xBDBF46), wxColour(0x5CB4FD)} ;
ColorScheme* colorScheme = new ColorScheme(colours, WXSIZEOF(colours));
// first step: create plot
PiePlot *plot = new PiePlot();
// create dataset
CategorySimpleDataset* dataset = new CategorySimpleDataset(categories, WXSIZEOF(categories));
// and add serie to it
dataset->AddSerie(_("Serie 1"), data, WXSIZEOF(data));
// create category renderer for legend drawing
dataset->SetRenderer(new CategoryRenderer(*colorScheme));
// set color scheme
plot->SetColorScheme(colorScheme);
plot->SetDataset(dataset);
// set legend to plot
plot->SetLegend(new Legend(wxCENTER, wxRIGHT));
GraphChartPanel->SetChart(new Chart(plot, "My First Chart"));
}
test::~test()
{
//(*Destroy(test)
//*)
}
doublemax wrote:Please show the complete sizer code.
Re: Using wxFreeChart in wxWidgets
Posted: Fri Aug 10, 2018 3:21 pm
by doublemax
First of all: A sizer that only contains one element usually doesn't make any sense. I assume you will add more elements later.
FlexGridSizer1 = new wxFlexGridSizer(0, 3, 0, 0);
A wxFlexGridSizer should not have "0" rows (first parameter)
For grid based sizers, the wxEXPAND flag is not enough, you also need to set which rows and columns are growable.
Try adding:
Code: Select all
AddGrowableCol(0, 1);
AddGrowableRow(0, 1);
Re: Using wxFreeChart in wxWidgets
Posted: Fri Aug 10, 2018 3:57 pm
by nona
doublemax wrote:First of all: A sizer that only contains one element usually doesn't make any sense. I assume you will add more elements later.
FlexGridSizer1 = new wxFlexGridSizer(0, 3, 0, 0);
A wxFlexGridSizer should not have "0" rows (first parameter)
For grid based sizers, the wxEXPAND flag is not enough, you also need to set which rows and columns are growable.
Try adding:
Code: Select all
AddGrowableCol(0, 1);
AddGrowableRow(0, 1);
Many Thanks
It works fine now