KiCad PCB EDA Suite
sim_plot_panel.cpp File Reference
#include "sim_plot_panel.h"
#include "sim_plot_frame.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

◆ countDecimalDigits()

static int countDecimalDigits ( double  x,
int  maxDigits 
)
static

Definition at line 101 of file sim_plot_panel.cpp.

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

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

◆ formatFloat()

static wxString formatFloat ( double  x,
int  nDigits 
)
static

Definition at line 32 of file sim_plot_panel.cpp.

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

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

◆ formatSILabels()

static void formatSILabels ( mpScaleBase scale,
const wxString &  aUnit,
int  nDigits 
)
static

Definition at line 123 of file sim_plot_panel.cpp.

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

References countDecimalDigits(), formatFloat(), getSISuffix(), and scale.

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().

◆ getSISuffix()

static void getSISuffix ( double  x,
const wxString &  unit,
int &  power,
wxString &  suffix 
)
static

Definition at line 52 of file sim_plot_panel.cpp.

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

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

◆ wxDEFINE_EVENT()

wxDEFINE_EVENT ( EVT_SIM_CURSOR_UPDATE  ,
wxCommandEvent   
)