tech-radar icon indicating copy to clipboard operation
tech-radar copied to clipboard

Add a legend to wxMath2DPlot

Open lhedjazi opened this issue 7 years ago • 4 comments

Need help for adding a legend to wxMath2DPlot.

Many thanks,

lhedjazi avatar Jan 14 '19 22:01 lhedjazi

Well, this type of chart hasn't associate with some string value and I'm not sure that it needed. So for now you can use code below:

wxVector<wxString> labels;//create vector for storing legend value
labels.push_back("first");//add as many string as many dataset was added to chartData
wxChartLegendData legendData;//create legend data
for(std::size_t i = 0; i<chartData.GetDatasets().size();i++)
{
    //append pair (color,text) to legend 
    legendData.Append(
        wxChartLegendItem(chartData.GetDatasets()[i]->GetLineColor(),
	labels[i]));
}
wxChartLegendCtrl* legendCtrl = new wxChartLegendCtrl(panel, wxID_ANY, legendData,
	wxDefaultPosition, wxDefaultSize, wxBORDER_NONE);
panelSizer->Add(legendCtrl, 1, wxEXPAND);//add to sizer

Kvaz1r avatar Jan 16 '19 12:01 Kvaz1r

Thanks for the code.

It is working but I have some troubles to control the size of the legend chart. It seems that when associated to the same panel, the panel is splitted into two part of the same size, one part for the wxMath2Dplot chart and the second blank part contain only the legend chart as you can see below. I failed to locate it on the top of the wxMath2Dplot but this is may be due to my limited knowledge of wxwidgets.

Thanks for your help.

image

lhedjazi avatar Jan 17 '19 21:01 lhedjazi

I failed to locate it on the top of the wxMath2Dplot but this is may be due to my limited knowledge of wxwidgets.

Usually and in the example too, controls are stored in sizers so you can choose their location by order of addition into sizer and by special arguments in method Add.

In your case:

panelSizer->Add(legendCtrl, 0, wxEXPAND);
panelSizer->Add(math2dPlotCtrl, 1, wxEXPAND);

For more information read the documentation and tutorials - Layout management in wxWidgets and Sizers.

Kvaz1r avatar Jan 18 '19 09:01 Kvaz1r

Thanks. Very helpful indeed.

Worked but I prefered this one:

legendSizer->Add(legendCtrl,0,wxEXPAND); panelSizer->Add(math2dPlotCtrl, 1, wxEXPAND); panelSizer->Add(legendSizer, 0, wxEXPAND);

image

Still trying to introduce X and Y label.

Many thanks,

lhedjazi avatar Jan 18 '19 22:01 lhedjazi