KiCad PCB EDA Suite
sim_plot_panel.cpp File Reference
#include "sim_plot_panel.h"
#include <algorithm>
#include <limits>

Go to the source code of this file.

Classes

class  FREQUENCY_LOG_SCALE
 
class  FREQUENCY_LIN_SCALE
 
class  TIME_SCALE
 
class  VOLTAGE_SCALE_X
 
class  GAIN_SCALE
 
class  PHASE_SCALE
 
class  VOLTAGE_SCALE_Y
 
class  CURRENT_SCALE
 

Functions

static wxString formatFloat (double x, int nDigits)
 
static void getSISuffix (double x, const wxString &unit, int &power, wxString &suffix)
 
static int countDecimalDigits (double x, int maxDigits)
 
static void formatSILabels (mpScaleBase *scale, const wxString &aUnit, int nDigits)
 
 wxDEFINE_EVENT (EVT_SIM_CURSOR_UPDATE, wxCommandEvent)
 

Function Documentation

static int countDecimalDigits ( double  x,
int  maxDigits 
)
static

Definition at line 100 of file sim_plot_panel.cpp.

Referenced by FREQUENCY_LOG_SCALE::formatLabels(), and formatSILabels().

101 {
102  int64_t k = (int)( ( x - floor( x ) ) * pow( 10.0, (double) maxDigits ) );
103  int n = 0;
104 
105  while( k && ( ( k % 10LL ) == 0LL || ( k % 10LL ) == 9LL ) )
106  {
107  k /= 10LL;
108  }
109 
110  n = 0;
111 
112  while( k != 0LL )
113  {
114  n++;
115  k /= 10LL;
116  }
117 
118  return n;
119 }
static wxString formatFloat ( double  x,
int  nDigits 
)
static

Definition at line 31 of file sim_plot_panel.cpp.

Referenced by FREQUENCY_LOG_SCALE::formatLabels(), and formatSILabels().

32 {
33  wxString rv, fmt;
34 
35  if( nDigits )
36  {
37  fmt = wxT( "%.0Nf" );
38  fmt[3] = '0' + nDigits;
39  }
40  else
41  {
42  fmt = wxT( "%.0f" );
43  }
44 
45  rv.Printf( fmt, x );
46 
47  return rv;
48 }
static void formatSILabels ( mpScaleBase scale,
const wxString &  aUnit,
int  nDigits 
)
static

Definition at line 122 of file sim_plot_panel.cpp.

References mpScaleBase::AbsVisibleMaxValue(), countDecimalDigits(), formatFloat(), getSISuffix(), max, and mpScaleBase::TickLabels().

Referenced by FREQUENCY_LIN_SCALE::formatLabels(), TIME_SCALE::formatLabels(), VOLTAGE_SCALE_X::formatLabels(), GAIN_SCALE::formatLabels(), PHASE_SCALE::formatLabels(), VOLTAGE_SCALE_Y::formatLabels(), and CURRENT_SCALE::formatLabels().

123 {
124  double maxVis = scale->AbsVisibleMaxValue();
125 
126  wxString suffix;
127  int power, digits = 0;
128 
129  getSISuffix( maxVis, aUnit, power, suffix );
130 
131  double sf = pow( 10.0, power );
132 
133  for( auto &l : scale->TickLabels() )
134  {
135  int k = countDecimalDigits( l.pos / sf, nDigits );
136 
137  digits = std::max( digits, k );
138  }
139 
140  for( auto &l : scale->TickLabels() )
141  {
142  l.label = formatFloat ( l.pos / sf, digits ) + suffix;
143  l.visible = true;
144  }
145 }
static wxString formatFloat(double x, int nDigits)
std::vector< TickLabel > & TickLabels()
Definition: mathplot.h:782
static int countDecimalDigits(double x, int maxDigits)
static void getSISuffix(double x, const wxString &unit, int &power, wxString &suffix)
#define max(a, b)
Definition: auxiliary.h:86
double AbsVisibleMaxValue() const
Definition: mathplot.h:762
static void getSISuffix ( double  x,
const wxString &  unit,
int &  power,
wxString &  suffix 
)
static

Definition at line 51 of file sim_plot_panel.cpp.

Referenced by FREQUENCY_LOG_SCALE::formatLabels(), and formatSILabels().

52 {
53  const int n_powers = 11;
54 
55  const struct
56  {
57  double exponent;
58  char suffix;
59  } powers[] =
60  {
61  { -18, 'a' },
62  { -15, 'f' },
63  { -12, 'p' },
64  { -9, 'n' },
65  { -6, 'u' },
66  { -3, 'm' },
67  { 0, 0 },
68  { 3, 'k' },
69  { 6, 'M' },
70  { 9, 'G' },
71  { 12, 'T' },
72  { 14, 'P' }
73  };
74 
75  power = 0;
76  suffix = unit;
77 
78  if( x == 0.0 )
79  return;
80 
81  for( int i = 0; i < n_powers - 1; i++ )
82  {
83  double r_cur = pow( 10, powers[i].exponent );
84 
85  if( fabs( x ) >= r_cur && fabs( x ) < r_cur * 1000.0 )
86  {
87  power = powers[i].exponent;
88 
89  if( powers[i].suffix )
90  suffix = wxString( powers[i].suffix ) + unit;
91  else
92  suffix = unit;
93 
94  return;
95  }
96  }
97 }
wxDEFINE_EVENT ( EVT_SIM_CURSOR_UPDATE  ,
wxCommandEvent   
)