Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VC10.0

C++ Header to Plot vector<float> as 2D Graphs and Arrays as Potential Line Maps to SVG Files

4.20/5 (3 votes)
9 Apr 2018CPOL 38.1K   608  
C++ header file to plot data in the form of x, y, z arrays and list as potential lines and graphs

Introduction

An undocumented C++ file to plot potential lines and 2D graphs as SVG files. The input format for 2D graphs is a vector. The input format for a potential line maps are XX[], YY[] and ZZ[] = f(XX[i], YY[i]).

I wrote the code to plot aerospace simulation results and other simulation results in half a day to speed up productivity and automate the documentation process.

The name of the x and y axis have to be rearranged, as they are simply being dropped somewhere onto the plot.

Implementation

To plot a vector<VAR> as graph, use the example below:

C++
SVG_clearPlotBuffer();  // clear the SVG temporary buffer

list<VAR> function;

function.push_back(0); // x 1
function.push_back(2); // y 1
function.push_back(4); // x 2
function.push_back(4); // y 2

// adds a list of points X,Y,X,Y ... with a function name to the SVG temporary buffer
SVG_appendPointListBufferAS2DPlot( function,
"TEST");

 // writes the temporary SVG buffer to an SVG file with NAME, X-Axis name, Y-Axis title.
writeSVGPointListBufferAS2DPlot("test.svg", 0,0,0,
     "Rubber Materials", "Elongation", "Stress [Pa]");

The output of the above code is:

Image 1

Clear the temporary plot buffer by calling:

C++
SVG_clearPlotBuffer(); 

Plot potential lines:

C++
VAR  XX[100]; //- X coordinates of 100 points
VAR  YY[100]; //- Y coordinates of 100 points
VAR  ZZ[100]; //  - Values of 100 points

Make sure the points have an equal distance to each other and that the indexes are arranged as followed:

3;6;9
2;5;8
1;4;7

Pseudo code to create valid input buffers:

for x to DIM x do
for y to DIM y do 
SET XX[x+y*DIM(x)] = x;
SET YY[x+y*DIM(x)] = y;  
SET ZZ[x+y*DIM(x)] = f(x,y); 
END 
END

To plot potential lines, call:

JavaScript
VAR pos[10] = {0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7,0.01,0.02};  // plot 10 valued labels 
                                                       // onto corresponding potential line positions 

XXYYZZArrays2SVGImage_potential_lines("test.svg",  
10 /* X DIM -> 10*10 */,
10 /* Y DIM -> 10*10 */,
XX,YY,ZZ, // input
"NAME",  /// name of the plot
"X-AXIX NAME",  /// x axis name
"Y-AXIS NAME",  /// y axis name
10 /* number of lines to show */, 
pos /* Add 10 valued labels onto the corresponding potential lines position (0..1; begin ... end). 
   The value can also be set to NULL to plot no value labels  ... */);

An example results in:

Image 2

Point of Interest

I wrote the code to create plots and speed up productivity.

History

  • Version 1.02

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)