Using wxFreeChart in wxWidgets Topic is solved

Talk here about issues with one of the components hosted at wxCode, or suggest features for it.
Post Reply
nona
Knows some wx things
Knows some wx things
Posts: 32
Joined: Fri Apr 20, 2018 2:49 pm

Using wxFreeChart in wxWidgets

Post 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);
User avatar
mill-j
Earned a small fee
Earned a small fee
Posts: 23
Joined: Sat Jun 02, 2018 3:21 pm

Re: Using wxFreeChart in wxWidgets

Post 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.
[-X Don't Complain, Change It, It's Open-Source!
nona
Knows some wx things
Knows some wx things
Posts: 32
Joined: Fri Apr 20, 2018 2:49 pm

Re: Using wxFreeChart in wxWidgets

Post by nona »

Thanks for your reply I will test your code.
Last edited by nona on Tue Aug 07, 2018 1:51 pm, edited 1 time in total.
nona
Knows some wx things
Knows some wx things
Posts: 32
Joined: Fri Apr 20, 2018 2:49 pm

Re: Using wxFreeChart in wxWidgets

Post by nona »

Thanks I got it worked now =D> :) Many thanks.
nona
Knows some wx things
Knows some wx things
Posts: 32
Joined: Fri Apr 20, 2018 2:49 pm

Re: Using wxFreeChart in wxWidgets

Post 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?
User avatar
mill-j
Earned a small fee
Earned a small fee
Posts: 23
Joined: Sat Jun 02, 2018 3:21 pm

Re: Using wxFreeChart in wxWidgets

Post 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. :D
[-X Don't Complain, Change It, It's Open-Source!
User avatar
doublemax
Moderator
Moderator
Posts: 19102
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Using wxFreeChart in wxWidgets

Post 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.
Use the source, Luke!
nona
Knows some wx things
Knows some wx things
Posts: 32
Joined: Fri Apr 20, 2018 2:49 pm

Re: Using wxFreeChart in wxWidgets

Post 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
Attachments
Screen Shot 2018-08-10 at 10.31.17 AM.png
Screen Shot 2018-08-10 at 10.31.17 AM.png (27.98 KiB) Viewed 8476 times
nona
Knows some wx things
Knows some wx things
Posts: 32
Joined: Fri Apr 20, 2018 2:49 pm

Re: Using wxFreeChart in wxWidgets

Post 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
Attachments
Screen Shot 2018-08-10 at 10.29.52 AM.png
Screen Shot 2018-08-10 at 10.29.52 AM.png (30.79 KiB) Viewed 8475 times
User avatar
doublemax
Moderator
Moderator
Posts: 19102
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Using wxFreeChart in wxWidgets

Post by doublemax »

Please show the complete sizer code.
Use the source, Luke!
nona
Knows some wx things
Knows some wx things
Posts: 32
Joined: Fri Apr 20, 2018 2:49 pm

Re: Using wxFreeChart in wxWidgets

Post 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.
User avatar
doublemax
Moderator
Moderator
Posts: 19102
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Using wxFreeChart in wxWidgets

Post 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);
Use the source, Luke!
nona
Knows some wx things
Knows some wx things
Posts: 32
Joined: Fri Apr 20, 2018 2:49 pm

Re: Using wxFreeChart in wxWidgets

Post 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 =D> It works fine now :D
Post Reply