eeschema: migrate SIM_PLOT_PANEL to improved wxMathPlot [wip]

This commit is contained in:
Tomasz Wlostowski 2016-08-11 14:41:49 +02:00 committed by Maciej Suminski
parent b9e31f6d3a
commit e5bf70996b
2 changed files with 217 additions and 32 deletions

View File

@ -28,6 +28,147 @@
#include <algorithm>
#include <limits>
static wxString formatFloat (double x, int nDigits)
{
wxString rv, fmt;
if(nDigits)
{
fmt = wxT("%.0Nf");
fmt[3] = '0' + nDigits;
} else {
fmt = wxT("%.0f");
}
rv.Printf(fmt, x);
return rv;
}
static wxString formatSI ( double x, const wxString& unit, int decimalDigits, double maxValue = 0.0, bool lockSuffix = false, char suffix = 0 )
{
const int n_powers = 11;
const struct { double exponent; char suffix; } powers[] = {
{-18,'a'},
{-15,'f'},
{-12,'p'},
{-9,'n'},
{-6,'u'},
{-3,'m'},
{0, 0},
{3, 'k'},
{6, 'M'},
{9, 'G'},
{12, 'T'},
{15, 'P'}
};
if ( x== 0.0)
{
return wxT("0") + unit;
}
for ( int i = 0; i <n_powers - 1;i++)
{
double r_cur = pow(10, powers[i].exponent);
bool rangeHit;
if (maxValue != 0.0)
rangeHit = fabs(maxValue) >= r_cur && fabs(maxValue) < r_cur * 1000.0 ;
else
rangeHit = fabs(x) >= maxValue && fabs(x) < maxValue * 1000.0 ;
if( (!lockSuffix && rangeHit) || (lockSuffix && suffix == powers[i].suffix ) )
{
double v = x / r_cur;
wxString rv;
rv = formatFloat ( v, decimalDigits );
if(powers[i].suffix)
rv += powers[i].suffix;
rv += unit;
return rv;
}
}
return wxT("?");
}
class FREQUENCY_SCALE : public mpScaleXLog
{
public:
FREQUENCY_SCALE(wxString name, int flags, bool ticks = false, unsigned int type = 0) :
mpScaleXLog ( name, flags, ticks ,type ) {};
const wxString getLabel( int n )
{
return formatSI ( m_labeledTicks[n], wxT("Hz"), 2 );
}
};
class TIME_SCALE : public mpScaleX
{
public:
TIME_SCALE(wxString name, int flags, bool ticks = false, unsigned int type = 0) :
mpScaleX ( name, flags, ticks ,type ) {};
const wxString getLabel( int n )
{
return formatSI ( m_labeledTicks[n], wxT("s"), 3, AbsVisibleMaxValue() );
}
};
class GAIN_SCALE : public mpScaleY
{
public:
GAIN_SCALE(wxString name, int flags, bool ticks = false, unsigned int type = 0) :
mpScaleY ( name, flags, ticks ) {};
const wxString getLabel( int n )
{
return formatSI ( m_labeledTicks[n], wxT("dB"), 1, AbsVisibleMaxValue(), true, 0 );
}
};
class PHASE_SCALE : public mpScaleY
{
public:
PHASE_SCALE(wxString name, int flags, bool ticks = false, unsigned int type = 0) :
mpScaleY ( name, flags, ticks ) {};
const wxString getLabel( int n )
{
return formatSI ( m_labeledTicks[n], wxT("\u00B0"), 1, AbsVisibleMaxValue(), true, 0 );
}
};
class VOLTAGE_SCALE : public mpScaleY
{
public:
VOLTAGE_SCALE(wxString name, int flags, bool ticks = false, unsigned int type = 0) :
mpScaleY ( name, flags, ticks ) {};
const wxString getLabel( int n )
{
return formatSI ( m_labeledTicks[n], wxT("V"), 3, AbsVisibleMaxValue() );
}
};
class CURRENT_SCALE : public mpScaleY
{
public:
CURRENT_SCALE(wxString name, int flags, bool ticks = false, unsigned int type = 0) :
mpScaleY ( name, flags, ticks ) {};
const wxString getLabel( int n )
{
return formatSI ( m_labeledTicks[n], wxT("A"), 3, AbsVisibleMaxValue() );
}
};
void CURSOR::Plot( wxDC& aDC, mpWindow& aWindow )
{
@ -100,17 +241,24 @@ SIM_PLOT_PANEL::SIM_PLOT_PANEL( SIM_TYPE aType, wxWindow* parent, wxWindowID id,
: mpWindow( parent, id, pos, size, style ), m_colorIdx( 0 ),
m_axis_x( nullptr ), m_axis_y1( nullptr ), m_axis_y2( nullptr ), m_type( aType )
{
EnableDoubleBuffer( true );
SetMargins( 10, 10, 10, 10 );
LimitView( true );
SetMargins(50, 80, 50, 80);
wxColour grey(96, 96, 96);
SetColourTheme(*wxBLACK, *wxWHITE, grey);
EnableDoubleBuffer(true);
UpdateAll();
switch( m_type )
{
case ST_AC:
m_axis_x = new mpScaleX( wxT( "frequency [Hz]" ), mpALIGN_BORDER_BOTTOM );
m_axis_y1 = new mpScaleY( wxT( "magnitude [V]" ), mpALIGN_BORDER_LEFT );
m_axis_y2 = new mpScaleY( wxT( "phase [rad]" ), mpALIGN_BORDER_RIGHT );
m_axis_x = new FREQUENCY_SCALE( wxT( "Frequency" ), mpALIGN_BOTTOM );
m_axis_y1 = new GAIN_SCALE( wxT( "Gain" ), mpALIGN_LEFT );
m_axis_y2 = new PHASE_SCALE( wxT( "Phase" ), mpALIGN_RIGHT );
m_axis_y2->SetMasterScale(m_axis_y1);
break;
#if 0
case ST_DC:
m_axis_x = new mpScaleX( wxT( "voltage [V]" ), mpALIGN_BORDER_BOTTOM );
@ -122,9 +270,13 @@ SIM_PLOT_PANEL::SIM_PLOT_PANEL( SIM_TYPE aType, wxWindow* parent, wxWindowID id,
m_axis_y1 = new mpScaleY( wxT( "noise [(V or A)^2/Hz]" ), mpALIGN_BORDER_LEFT );
break;
#endif
case ST_TRANSIENT:
m_axis_x = new mpScaleX( wxT( "time [s]" ), mpALIGN_BORDER_BOTTOM );
m_axis_y1 = new mpScaleY( wxT( "voltage [V]" ), mpALIGN_BORDER_LEFT );
m_axis_x = new TIME_SCALE( wxT( "Time" ), mpALIGN_BOTTOM );
m_axis_y1 = new VOLTAGE_SCALE( wxT( "Voltage" ), mpALIGN_LEFT );
m_axis_y2 = new CURRENT_SCALE( wxT( "Current" ), mpALIGN_RIGHT );
m_axis_y2->SetMasterScale(m_axis_y1);
break;
default:
@ -150,13 +302,15 @@ SIM_PLOT_PANEL::SIM_PLOT_PANEL( SIM_TYPE aType, wxWindow* parent, wxWindowID id,
AddLayer( m_axis_y2 );
}
m_coords = new mpInfoCoords( wxRect( 0, 0, 100, 40 ), wxWHITE_BRUSH );
AddLayer( m_coords );
m_topLevel.push_back( m_coords );
m_legend = new mpInfoLegend( wxRect( 0, 40, 200, 40 ), wxTRANSPARENT_BRUSH );
m_legend = new mpInfoLegend( wxRect( 0, 40, 40, 40 ), wxWHITE_BRUSH );
AddLayer( m_legend );
m_topLevel.push_back( m_legend );
SetColourTheme(*wxBLACK, *wxWHITE, grey);
EnableDoubleBuffer(true);
UpdateAll();
}
@ -193,15 +347,31 @@ bool SIM_PLOT_PANEL::AddTrace( const wxString& aSpiceName, const wxString& aName
if( addedNewEntry )
{
// New entry
t = new TRACE( aName, aSpiceName );
t->SetPen( wxPen( generateColor(), 1, wxSOLID ) );
switch ( m_type )
{
case ST_TRANSIENT:
t = new TRACE_TRANSIENT( aName, aSpiceName );
break;
case ST_AC:
t = new TRACE_FREQ_RESPONSE( aName, aSpiceName );
break;
default:
assert(false);
}
assert(m_axis_x);
assert(m_axis_y1);
t->SetData( std::vector<double>( aT, aT + aPoints ), std::vector<double>( aY, aY + aPoints ) );
t->SetScale ( m_axis_x, m_axis_y1 );
t->SetPen( wxPen( generateColor(), 2, wxSOLID ) );
m_traces[aName] = t;
// It is a trick to keep legend & coords always on the top
for( mpLayer* l : m_topLevel )
DelLayer( l );
AddLayer( t );
AddLayer( (mpLayer *) t );
for( mpLayer* l : m_topLevel )
AddLayer( l );
@ -211,7 +381,6 @@ bool SIM_PLOT_PANEL::AddTrace( const wxString& aSpiceName, const wxString& aName
t = prev->second;
}
t->SetData( std::vector<double>( aT, aT + aPoints ), std::vector<double>( aY, aY + aPoints ) );
UpdateAll();
return addedNewEntry;
@ -289,7 +458,7 @@ wxColour SIM_PLOT_PANEL::generateColor()
/// http://stanford.edu/~mwaskom/software/seaborn/tutorial/color_palettes.html
/// https://github.com/Gnuplotting/gnuplot-palettes
const unsigned long colors[] = { 0x000080, 0x008000, 0x800000, 0x008080, 0x800080, 0x808000, 0x808080 };
const unsigned long colors[] = { 0x0000ff, 0x00ff00, 0xff0000, 0x00ffff, 0xff00ff, 0xffff000, 0xffffff };
//const unsigned long colors[] = { 0xe3cea6, 0xb4781f, 0x8adfb2, 0x2ca033, 0x999afb, 0x1c1ae3, 0x6fbffd, 0x007fff, 0xd6b2ca, 0x9a3d6a };

View File

@ -90,20 +90,8 @@ private:
class TRACE : public mpFXYVector
{
public:
TRACE( const wxString& aName, const wxString& aSpiceName )
: mpFXYVector( aName ), m_spiceName( aSpiceName ), m_cursor( nullptr )
{
SetContinuity( true );
ShowName( false );
}
void SetData( const std::vector<double>& aXs, const std::vector<double>& aYs )
{
mpFXYVector::SetData( aXs, aYs );
if( m_cursor )
m_cursor->Update();
}
TRACE( const wxString& aSpiceName ) :
m_spiceName( aSpiceName ), m_cursor( nullptr ) {};
const wxString& GetSpiceName() const
{
@ -135,11 +123,39 @@ public:
return m_cursor;
}
private:
protected:
wxString m_spiceName;
CURSOR* m_cursor;
};
class TRACE_FREQ_RESPONSE : public TRACE, public mpFSemiLogXVector
{
public:
TRACE_FREQ_RESPONSE( const wxString& aName, const wxString& aSpiceName )
: mpFSemiLogXVector( aName ), TRACE( aSpiceName )
{
mpFSemiLogXVector::SetContinuity( true );
mpFSemiLogXVector::SetDrawOutsideMargins( false );
mpFSemiLogXVector::ShowName( false );
}
};
class TRACE_TRANSIENT : public TRACE
{
public:
TRACE_TRANSIENT( const wxString& aName, const wxString& aSpiceName ) :
TRACE( aSpiceName )
{
mpFXYVector::SetName ( aName ); // hack
SetContinuity( true );
SetDrawOutsideMargins( false );
ShowName( false );
}
};
class SIM_PLOT_PANEL : public mpWindow
{
@ -243,7 +259,7 @@ private:
// Traces to be plotted
std::map<wxString, TRACE*> m_traces;
mpScaleX* m_axis_x;
mpScaleXBase* m_axis_x;
mpScaleY* m_axis_y1;
mpScaleY* m_axis_y2;
mpInfoLegend* m_legend;