I'm trying to export application data as xls chart.
I'm doing it using SimpleXlsxWriter.
But I have some problems with this lib.
Here's what I got using this lib
#include <Xlsx/Workbook.h>
int main()
{
using namespace SimpleXlsx;
CWorkbook book;
CWorksheet &data = book.AddSheet("Data");
std::vector<CellDataDbl> header = { 1, 3, 5, 6, 8 };
std::vector<CellDataDbl> data1 = { 2, 6, 4, 8, 5 };
std::vector<CellDataDbl> data2 = { 5, 3, 5, 2, 4 };
data.AddRow(header);
data.AddRow(data1);
data.AddRow(data2);
CChartsheet &chart = book.AddChart("Chart", CHART_BAR);
chart.SetBarDirection(CChartsheet::BAR_DIR_VERTICAL);
chart.SetBarGrouping(CChartsheet::BAR_GROUP_STACKED);
chart.SetTableDataState(CChartsheet::TBL_DATA);
chart.SetLegendPos(CChartsheet::EPosition::POS_TOP);
CChartsheet::Series ser1;
ser1.valAxisFrom = CellCoord(1, 0);
ser1.valAxisTo = CellCoord(1, data1.size() - 1);
ser1.valSheet = &data;
ser1.title = "Ser1";
CChartsheet::Series ser2;
ser2.valAxisFrom = CellCoord(2, 0);
ser2.valAxisTo = CellCoord(2, data2.size() - 1);
ser2.valSheet = &data;
ser2.title = "Ser2";
chart.AddSeries(ser1);
chart.AddSeries(ser2);
book.Save("test.xls");
}
I didn't find the ways how to show y-axes, show horizontal lines, set x-axes values, insert the chart into the data sheet, etc.
Here's what do I want to get as a result
What a library can help me or how can I get a required result? Maybe SimpleXlsxWriter can do that?
You can get what you want from the SimpleXlsxWriter, just with a few modifications of your code. Briefly:
save with SimpleXlsxWriter's format .xlsx not .xls
Add a category axis to one of your series and set it to first data row
use BAR_GROUP_PERCENT_STACKED since you want the y axis to be on % scale
You can control the order you add the series the the chart, to get the order you want in the bottom table
Consider this code, suggested modifications are indicated in comments:
int main()
{
using namespace SimpleXlsx;
CWorkbook book;
CWorksheet &data = book.AddSheet("Data");
std::vector<CellDataDbl> header = { 1, 3, 5, 6, 8 };
std::vector<CellDataDbl> data1 = { 2, 6, 4, 8, 5 };
std::vector<CellDataDbl> data2 = { 5, 3, 5, 2, 4 };
data.AddRow(header);
data.AddRow(data1);
data.AddRow(data2);
CChartsheet &chart = book.AddChart("Chart", CHART_BAR);
chart.SetBarDirection(CChartsheet::BAR_DIR_VERTICAL);
chart.SetBarGrouping(CChartsheet::BAR_GROUP_PERCENT_STACKED); // <-- seems you want this format?
chart.SetTableDataState(CChartsheet::TBL_DATA);
chart.SetLegendPos(CChartsheet::EPosition::POS_TOP);
chart.SetYAxisGrid(CChartsheet::EGridLines::GRID_MAJOR); // <-- to draw the horizontal lines
CChartsheet::Series ser1;
ser1.valSheet = &data;
ser1.valAxisFrom = CellCoord(1, 0);
ser1.valAxisTo = CellCoord(1, data1.size() - 1);
ser1.title = "Ser1";
// Now add a category axis from your the first row of the data sheet
ser1.catAxisFrom = CellCoord(0, 0);
ser1.catAxisTo = CellCoord(0, data1.size() - 1);
ser1.catSheet = &data;
CChartsheet::Series ser2;
ser2.valSheet = &data;
ser2.valAxisFrom = CellCoord(2, 0);
ser2.valAxisTo = CellCoord(2, data2.size() - 1);
ser2.title = "Ser2";
// insert the series in the order you want from the bottom up in the chart's table
chart.AddSeries(ser2);
chart.AddSeries(ser1);
book.Save("c:\\so\\test.xlsx"); // <-- Save as xlsx, not xls
}
Related
I'm trying to adapt a c++ to dart, and I ran into this situation with enum, assigning default values ββI think. follow the code
enum skills_t : uint8_t {
SKILL_FIST = 0,
SKILL_CLUB = 1,
SKILL_SWORD = 2,
SKILL_AXE = 3,
SKILL_DISTANCE = 4,
SKILL_SHIELD = 5,
SKILL_FISHING = 6,
SKILL_CRITICAL_HIT_CHANCE = 7,
SKILL_CRITICAL_HIT_DAMAGE = 8,
SKILL_LIFE_LEECH_CHANCE = 9,
SKILL_LIFE_LEECH_AMOUNT = 10,
SKILL_MANA_LEECH_CHANCE = 11,
SKILL_MANA_LEECH_AMOUNT = 12,
SKILL_MAGLEVEL = 13,
SKILL_LEVEL = 14,
SKILL_FIRST = SKILL_FIST,
SKILL_LAST = SKILL_MANA_LEECH_AMOUNT
};
}
uint32_t skillBase[SKILL_LAST + 1] = {50, 50, 50, 50, 30, 100, 20};
Is it possible to adapt this code to dart/flutter?
I would like to replicate the same operation in dart, it seems that he assigned these values ββto each enum in a range
Yes, it is possible to adapt this code to Dart/Flutter.
In Dart, you can use the enum keyword to define an enumeration. The syntax is similar to C++, but there is no need to specify a type like uint8_t.
Regarding the default values, you can initialize the enum members with a value like in C++.
Here is an example of how the C++ code could be adapted to Dart:
enum Skills {
FIST,
CLUB,
SWORD,
AXE,
DISTANCE,
SHIELD,
FISHING,
CRITICAL_HIT_CHANCE,
CRITICAL_HIT_DAMAGE,
LIFE_LEECH_CHANCE,
LIFE_LEECH_AMOUNT,
MANA_LEECH_CHANCE,
MANA_LEECH_AMOUNT,
MAGLEVEL,
LEVEL,
FIRST = FIST,
LAST = MANA_LEECH_AMOUNT,
}
final List<int> skillBase = [
50, 50, 50, 50, 30, 100, 20
];
You can also use a Map to assign the default values to each enum member.
enum Skills {
FIST,
CLUB,
SWORD,
AXE,
DISTANCE,
SHIELD,
FISHING,
CRITICAL_HIT_CHANCE,
CRITICAL_HIT_DAMAGE,
LIFE_LEECH_CHANCE,
LIFE_LEECH_AMOUNT,
MANA_LEECH_CHANCE,
MANA_LEECH_AMOUNT,
MAGLEVEL,
LEVEL,
FIRST = FIST,
LAST = MANA_LEECH_AMOUNT,
}
final Map<Skills, int> skillBase = {
Skills.FIST: 50,
Skills.CLUB: 50,
Skills.SWORD: 50,
Skills.AXE: 50,
Skills.DISTANCE: 30,
Skills.SHIELD: 100,
Skills.FISHING: 20,
// Add the rest of the skills
};
Both the above examples will work fine in dart/flutter.
I want to turn the two lists (shown below) into a table sort of like this:
Faults | Severity
motor velocity: 7 | 3
Using faultItems[i] with corresponding severityList[i]. I want to be able to show the table in a Flutter app using dart. Any help is greatly appreciated!
faultItems list
[motor velocity: 7, motor velocity: 2, motor temp: 6, motor temp: 3, motor temp: 9, motor temp: 4, motor temp: 7, motor temp: 8, motor temp: 2, motor temp: 5, no motor comm]
severityList list
[3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 3]
code:
List<int> severityList = [0];
List faultItems = [0];
var lastFault;
var pairs = new Map<String, int>();
Widget _buildFaultOutput(snapshot) {
severityList.clear();
faultItems.clear();
//New growable lists with 0 set as the first value to prevent error
var msg = snapshot.data;
var faultList = msg["fault_list"];
var numberOfFaults = faultList.length;
if (faultList.length == 0) {
return Text("None");
} else {
for (var i = 0; i < faultList.length; i++) {
// print(faults);
// print("i: $i");
var faults = faultList[i];
var motorCurrent = faults["fault"];
var severity = faults["severity"];
if (motorCurrent != lastFault) {
faultItems.insert(i, motorCurrent);
severityList.insert(i, severity);
// print('added ${faultItems[i]}, severity: ${severityList[i]}');
lastFault = motorCurrent;
} else {}
//TODO: add list of faults with key/value pairs (fault:severity): Text of "motorCurrent" in color of severity
//TODO: Print each individual item from list with severity
// return Text(
// "Number of Faults: ${faultList.length}\n$faultItems\nSeverity: ${severityList[i]}");
for (var i = 0; i < faultItems.length; i++) {
pairs[faultItems[i]] = severityList[i];
}
print('$faultItems');
print('$severityList');
}
return Text(
"Pairs: $pairs\nNumber of Faults: ${faultList.length}\n${faultItems}\nSeverity: ${severityList}");
}
}
I want to modify values for a matrix that I previously don't its size:
ArrayFactory factory;
std::vector<size_t> position = {3, 14, 2, 23};
Array mat_data = factory.createArray<double>({30, 30, 30, 30});
Reference<TypedArray<double>> temp = mat_data[position[0]]; //fails here
for (int i = 1; i < positions.size(); ++i)
mat_temp = mat_temp[positions[i]];
mat_temp[positions.back()] = getMyValue(position);
This gives me the following error:
Not enough indices provided
I know that I'm able to do this:
mat_data[3][14][2][23] = getMyValue(position);
But I can't hard code this, because I don't know the size of position when the algorithm starts.
Actual solution removing the private that hides ArrayElementRef(ArrayElementRef<is_const_ref>&& rhs) MW_NOEXCEPT on ArrayElementRef.hpp:
ArrayElementRef<false> getElementRef(ArrayElementRef<false> elem, std::vector<size_t>& position, size_t pos) {
if (pos >= position.size() - 1)
return std::move(elem);
return getElementRef(elem[position[pos]], position, ++pos);
}
And then:
ArrayFactory factory;
std::vector<size_t> position = {3, 14, 2, 23};
Array mat_data = factory.createArray<double>({30, 30, 30, 30});
ArrayElementRef<false> mat_temp = getElementRef(mat_data[position[0]], positions, 1);
mat_temp[position.back()] = getMyValue(position);
But, removing this private is not desirable...
I need to plot line segments (not continous) combined in one chart.
In order to create a plot I use the code from the VTK example but when I skip some values when filling vtkTable line goes down to zero value. And if creating another plot and skip some of the first elements the line goes up from zero. So I need to solve these issues.
I have figured out the solution myself. It appears very simple at last. The basic idea is to represent each line segment by its own plot and to use a separate table for each of the plots. One more improtant point all of the tables do not need to have the same step on the X-axis.
Here is the sample code:
vtkSmartPointer<vtkContextView> view = vtkSmartPointer<vtkContextView>::New();
view->GetRenderer()->SetBackground(1.0, 1.0, 1.0);
vtkSmartPointer<vtkChartXY> chart = vtkSmartPointer<vtkChartXY>::New();
vtkSmartPointer<vtkTable> table1 = vtkSmartPointer<vtkTable>::New();
vtkSmartPointer<vtkFloatArray> xAxis1 = vtkSmartPointer<vtkFloatArray>::New();
xAxis1->SetName("X");
vtkSmartPointer<vtkFloatArray> yAxis1 = vtkSmartPointer<vtkFloatArray>::New();
yAxis1->SetName("");
table1->AddColumn(xAxis1);
table1->AddColumn(yAxis1);
table1->SetNumberOfRows(2);
table1->SetValue(0, 0, 1);
table1->SetValue(0, 1, 1);
table1->SetValue(1, 0, 2);
table1->SetValue(1, 1, 2);
vtkPlot* plot1 = chart->AddPlot(vtkChart::LINE);
plot1->SetInputData(table1, 0, 1);
plot1->SetColor(0, 0, 255, 255);
vtkSmartPointer<vtkTable> table2 = vtkSmartPointer<vtkTable>::New();
vtkSmartPointer<vtkFloatArray> xAxis2 = vtkSmartPointer<vtkFloatArray>::New();
xAxis2->SetName("X");
vtkSmartPointer<vtkFloatArray> yAxis2 = vtkSmartPointer<vtkFloatArray>::New();
yAxis2->SetName("");
table2->AddColumn(xAxis2);
table2->AddColumn(yAxis2);
table2->SetNumberOfRows(3);
table2->SetValue(0, 0, 4);
table2->SetValue(0, 1, 4);
table2->SetValue(1, 0, 5);
table2->SetValue(1, 1, 5);
table2->SetValue(2, 0, 6);
table2->SetValue(2, 1, 6);
vtkPlot* plot2 = chart->AddPlot(vtkChart::LINE);
plot2->SetInputData(table2, 0, 1);
plot2->SetColor(255, 0, 0, 255);
view->GetScene()->AddItem(chart);
view->GetInteractor()->Initialize();
view->GetInteractor()->Start();
P.S. You can also specify several rows with the same x coordinate.
I can't seem to get the implementation of my member list correct. I want to DEFAULT initialize my Set members nyX and nyY, however I keep getting an error.
class Location
{
public:
vector<int> nyXv = { 0, 1, 2, 3, 4, 5};
vector<int> nyYv = { 0, 1, 2, 3, 4, 5 };
Set nyX(vector<int>);
Set nyY(vector<int>);
Location();
~Location();
};
Location::Location()
:nyX(nyXv), nyY(nyYv)
{
}
Look at this example
You can initialize you vectors like this:
class Location
{
public:
vector<int> nyXv;// = { 0, 1, 2, 3, 4, 5};
vector<int> nyYv;// = { 0, 1, 2, 3, 4, 5 };
///...
Location();
~Location();
};
static const int arrX[] = {0, 1, 2, 3, 4, 5};
static const int arrY[] = {0, 1, 2, 3, 4, 5};
Location::Location()
:nyXv(arrX, arrX + sizeof(arrX) / sizeof(arrX[0]) )
,nyYv(arrY, arrY + sizeof(arrY) / sizeof(arrY[0]))
{
}
P.S. Of course there are many ways to improve this code but it should give you an idea