Fixed simulation plot legend

This commit is contained in:
Maciej Suminski 2016-08-11 14:41:14 +02:00
parent 0261a0e59c
commit 781a12222c
2 changed files with 54 additions and 5 deletions

View File

@ -68,6 +68,7 @@ void SIM_PLOT_PANEL::AddTrace( const wxString& aName, int aPoints,
TRACE trace;
trace.name = aName;
trace.style = wxString( '-' ) + m_painter.GenerateColor( SIM_PLOT_PAINTER::DARK );
trace.x.Set( aT, aPoints );
trace.y.Set( aY, aPoints );
m_traces.push_back( trace );
@ -141,12 +142,54 @@ int SIM_PLOT_PAINTER::Draw( mglGraph* aGraph )
// Draw traces
for( auto t : traces )
{
aGraph->AddLegend( (const char*) t.name.c_str(), "" );
aGraph->Plot( t.y, "-" );
aGraph->AddLegend( (const char*) t.name.c_str(), t.style );
aGraph->Plot( t.y, t.style );
}
if( traces.size() )
aGraph->Legend( 1, "-#" );
aGraph->Legend( 1, "-#" ); // legend entries horizontally + draw a box around legend
return 0;
}
wxString SIM_PLOT_PAINTER::GenerateColor( COLOR_TYPE aType )
{
const char colors[] = "rgbcmylenupq";
const unsigned int colorsNumber = sizeof( colors ) - 1;
// Safe defaults
char color = 'k'; // black
int shade = 5;
switch( aType )
{
case LIGHT:
color = colors[m_lightColorIdx % colorsNumber];
shade = 5 + m_lightColorIdx / colorsNumber;
++m_lightColorIdx;
if( shade == 10 )
{
// Reached the color limit
shade = 5;
m_lightColorIdx = 0;
}
break;
case DARK:
color = toupper( colors[m_darkColorIdx % colorsNumber] );
shade = 5 - m_darkColorIdx / colorsNumber;
++m_darkColorIdx;
if( shade == 0 )
{
// Reached the color limit
shade = 5;
m_darkColorIdx = 0;
}
break;
}
return wxString::Format( "{%c%d}", color, shade );
}

View File

@ -33,7 +33,7 @@ class SIM_PLOT_PAINTER : public mglDraw
{
public:
SIM_PLOT_PAINTER( const SIM_PLOT_PANEL* aParent )
: m_parent( aParent )
: m_parent( aParent ), m_lightColorIdx( 0 ), m_darkColorIdx( 0 )
{
}
@ -45,8 +45,14 @@ public:
int Draw( mglGraph* aGraph ) override;
enum COLOR_TYPE { LIGHT, DARK };
///> Generates a new, unique color for plotting curves
wxString GenerateColor( COLOR_TYPE aType );
private:
const SIM_PLOT_PANEL* m_parent;
int m_lightColorIdx, m_darkColorIdx;
};
@ -59,7 +65,7 @@ public:
~SIM_PLOT_PANEL();
struct TRACE {
wxString name;
wxString name, style;
mglData x, y;
};