-
Notifications
You must be signed in to change notification settings - Fork 1
/
charthelper.cpp
379 lines (300 loc) · 10 KB
/
charthelper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
///////////////////////////////////////////////////////////////////////////////
// Project: wxECharts
// Home: https://github.com/PBfordev/wxecharts
// File Name: charthelper.cpp
// Purpose: Implementation of helper class using Apache ECharts
// Author: PB
// Created: 2024-08-22
// Copyright: (c) 2024 PB
// Licence: wxWindows licence
///////////////////////////////////////////////////////////////////////////////
#include <wx/wx.h>
#include <wx/webview.h>
#include <utility>
#include <json.hpp>
#include "charthelper.h"
using namespace std;
using json = nlohmann::ordered_json;
ChartHelper::ChartHelper()
{}
void ChartHelper::SetWebView(wxWebView* webView)
{
wxASSERT(webView);
m_webView = webView;
}
size_t ChartHelper::GetVariableNamesCount() const
{
return m_variableNames.size();
}
bool ChartHelper::GetVariableName(const size_t nameIdx, wxString& name) const
{
wxCHECK(nameIdx < m_variableNames.size(), false);
name = m_variableNames[nameIdx];
return true;
}
vector<wxString> ChartHelper::GetVariableNames() const
{
return m_variableNames;
}
bool ChartHelper::AddVariableName(const wxString& name)
{
wxCHECK_MSG(!m_series.empty(), false, "Adding variable name after adding a series");
for ( const auto& n : m_variableNames)
wxCHECK_MSG(!n.IsSameAs(name, true), false, "Variable name already used");
m_variableNames.push_back(name);
return true;
}
bool ChartHelper::AddVariableNames(const std::vector<wxString>& names)
{
wxCHECK(!names.empty(), false);
wxCHECK_MSG(m_series.empty(), false, "Adding variable name after adding a series");
for ( const auto& vn : m_variableNames)
{
for ( const auto & n : names)
wxCHECK_MSG(!n.IsSameAs(vn, true), false, "Variable name already used");
}
m_variableNames.insert(m_variableNames.end(), names.begin(), names.end());
return true;
}
bool ChartHelper::SetVariableName(const size_t nameIdx, const wxString& name)
{
wxCHECK(!name.empty(), false);
wxCHECK(nameIdx < m_variableNames.size(), false);
for ( size_t i = 0; i < m_variableNames.size(); ++i )
{
if ( i == nameIdx )
continue;
wxCHECK_MSG(!m_variableNames[i].IsSameAs(name, true), false, "Variable name already used");
}
m_variableNames[nameIdx] = name;
return true;
}
size_t ChartHelper::GetSeriesCount() const
{
return m_series.size();
}
bool ChartHelper::AddSeries(const ValueSeries& series)
{
wxCHECK_MSG(!m_variableNames.empty(), false, "Adding series before adding variable names");
wxCHECK(!series.name.empty(), false);
wxCHECK(series.data.size() == m_variableNames.size(), false);
for ( const auto& s : m_series )
wxCHECK_MSG(!s.name.IsSameAs(series.name, true), false, "Series name already used");
m_series.push_back(series);
return true;
}
bool ChartHelper::GetSeriesName(const size_t seriesIdx, wxString& name) const
{
wxCHECK(seriesIdx < m_series.size(), false);
name = m_series[seriesIdx].name;
return true;
}
vector<wxString> ChartHelper::GetSeriesNames() const
{
vector<wxString> names;
names.reserve(m_series.size());
for ( const auto& s : m_series )
names.push_back(s.name);
return names;
}
bool ChartHelper::SetSeriesName(const size_t seriesIdx, const wxString& name)
{
wxCHECK(!name.empty(), false);
wxCHECK(seriesIdx < m_series.size(), false);
for ( size_t i = 0; i < m_series.size(); ++i)
{
if ( i == seriesIdx )
continue;
wxCHECK_MSG(!m_series[i].name.IsSameAs(name, true), false, "Series name already used");
}
m_series[seriesIdx].name = name;
return true;
}
bool ChartHelper::GetSeriesType(const size_t seriesIdx, SeriesType& type) const
{
wxCHECK(seriesIdx < m_series.size(), false);
type = m_series[seriesIdx].type;
return true;
}
bool ChartHelper::SetSeriesType(const size_t seriesIdx, const SeriesType& type)
{
wxCHECK(seriesIdx < m_series.size(), false);
m_series[seriesIdx].type = type;
return true;
}
bool ChartHelper::GetSeriesData(const size_t seriesIdx, std::vector<double>& data) const
{
wxCHECK(seriesIdx < m_series.size(),false);
data = m_series[seriesIdx].data;
return true;
}
bool ChartHelper::SetSeriesData(const size_t seriesIdx, const std::vector<double>& data)
{
wxCHECK(seriesIdx < m_series.size(), false);
wxCHECK(data.size() == m_variableNames.size(), false);
m_series[seriesIdx].data = data;
return true;
}
void ChartHelper::RunChartCreate()
{
wxCHECK_RET(m_webView, "m_webView is null");
m_webView->RunScriptAsync("wxEChartsCreateChart('chart');", (void*)CreateChart);
}
void ChartHelper::RunChartUpdateSeries()
{
wxCHECK_RET(m_webView, "m_webView is null");
wxCHECK_RET(!m_series.empty(), "m_series is empty");
wxString script;
try
{
vector<json> allSeriesJSON;
for ( const auto& s : m_series )
{
json oneSeriesJSON;
oneSeriesJSON["name"] = s.name.utf8_string();
if ( s.type == Bar )
oneSeriesJSON["type"] = "bar";
else
oneSeriesJSON["type"] = "line";
oneSeriesJSON["data"] = s.data;
allSeriesJSON.push_back(oneSeriesJSON);
}
json j;
j["series"] = allSeriesJSON;
script.Printf("wxEChartsUpdateSeries('%s');", wxString::FromUTF8(j.dump()));
}
catch (const json::exception& e)
{
wxLogError(_("JSON error in %s (%s)."), __FUNCTION__, e.what());
return;
}
m_webView->RunScriptAsync(script, (void*)UpdateSeries);
}
void ChartHelper::RunChartUpdateVariableNames()
{
wxCHECK_RET(m_webView, "m_webView is null");
wxCHECK_RET(!m_variableNames.empty(), "m_variableNames is empty");
wxString script;
try
{
json j;
for ( const auto& n : m_variableNames )
j.push_back(n.utf8_string());
script.Printf("wxEChartsUpdateVariableNames('%s');", wxString::FromUTF8(j.dump()));
}
catch (const json::exception& e)
{
wxLogError(_("JSON error in %s (%s)."), __FUNCTION__, e.what());
return;
}
m_webView->RunScriptAsync(script, (void*)UpdateVariableNames);
}
void ChartHelper::RunChartGetColors()
{
wxCHECK_RET(m_webView, "m_webView is null");
m_webView->RunScriptAsync("wxEChartsGetChartColors();", (void*)GetColors);
}
void ChartHelper::RunChartSetColors(const std::vector<wxColour>& colors)
{
wxCHECK_RET(m_webView, "m_webView is null");
wxString script;
try
{
json j = json::array();
for ( const auto& c : colors )
{
j.push_back(c.GetAsString(wxC2S_HTML_SYNTAX).utf8_string());
}
script.Printf("wxEChartsSetChartColors('%s')", j.dump());
}
catch (const json::exception& e)
{
wxLogError(_("JSON error in %s (%s)."), __FUNCTION__, e.what());
return;
}
m_webView->RunScriptAsync(script, (void*)SetColors);
}
void ChartHelper::RunChartGetSizingOptions()
{
wxCHECK_RET(m_webView, "m_webView is null");
wxString script;
m_webView->RunScriptAsync("wxEChartsGetChartSizingOptions();", (void*)GetSizingOptions);
}
void ChartHelper::RunChartSetSizingOptions(const double widthToHeightRatio, const int minWidth, const int minHeight)
{
wxCHECK_RET(m_webView, "m_webView is null");
wxString script;
try
{
json j;
j["widthToHeightRatio"] = widthToHeightRatio;
j["minWidth"] = minWidth;
j["minHeight"] = minHeight;
script.Printf("wxEChartsSetChartSizingOptions('%s');", j.dump());
}
catch (const json::exception& e)
{
wxLogError(_("JSON error in %s (%s)."), __FUNCTION__, e.what());
return;
}
m_webView->RunScriptAsync(script, (void*)SetSizingOptions);
}
void ChartHelper::RunChartGetPNG(const int imageWidth)
{
wxCHECK_RET(m_webView, "m_webView is null");
wxString script;
script.Printf("wxEChartsSaveChartAsImage(%d);", imageWidth);
m_webView->RunScriptAsync(script, (void*)GetPNG);
}
void ChartHelper::RunChartGetEChartsVersion()
{
wxCHECK_RET(m_webView, "m_webView is null");
m_webView->RunScriptAsync("wxEChartsGetEChartsVersion();", (void*)GetEChartsVersion);
}
bool ChartHelper::JSONToColors(const wxString& JSONStr, std::vector<wxColour>& colors)
{
try
{
const json j = json::parse(string(JSONStr.utf8_string()));
if ( !j.is_array() )
{
wxLogError(_("Invalid JSON colors string in %s."), __FUNCTION__);
return false;
}
vector<wxColor> tmpColors;
for ( const auto& e : j )
{
wxColour color(wxString::FromUTF8(e.get<string>()));
if ( !color.IsOk() )
{
wxLogError(_("Invalid JSON color in %s."), __FUNCTION__);
return false;
}
tmpColors.emplace_back(move(color));
}
colors = move(tmpColors);
}
catch (const json::exception& e)
{
wxLogError(_("JSON parsing error in %s (%s)."), __FUNCTION__, e.what());
return false;
}
return true;
}
bool ChartHelper::JSONToSizingOptions(const wxString& JSONStr, double& widthToHeightRatio,
int& minWidth, int& minHeight)
{
try
{
const json j = json::parse(string(JSONStr.utf8_string()));
widthToHeightRatio = j.at("widthToHeightRatio").get<double>();
minWidth = j.at("minWidth").get<int>();
minHeight = j.at("minHeight").get<int>();
}
catch (const json::exception& e)
{
wxLogError(_("JSON parsing error in %s (%s)."), __FUNCTION__, e.what());
return false;
}
return true;
}