Chart Generation and Rendering in OpenGL
#include
#include
#include
#include
#include
#include
#include
using namespace std;
// window dimensions
#define WINDOW_HEI 500
#define WINDOW_WID 500
// Color of the grid lines
#define GRID_R 1.0
#define GRID_G 1.0
#define GRID_B 1.0
// thickness of grid lines
#define GRID_THICK 1
// color of the chart
#define CHART_R 0.5
#define CHART_G 1.0
#define CHART_B 0.5
// default plot point size
#define POINT_SIZE 6
// default plot line thickness
#define LINE_THICK 6
// Definition of a box
typedef struct
{
int x0, y0, x1, y1;
}Box;
/**
* Generate command for setting color
*/
void set_color(ofstream &out, double r, double g, double b)
{
out << "set_color " << fixed << setprecision(1) << r << " " << g << " " << b < data)
{
string filename = "column.txt";
cout << "Saving chart in " << filename << "..." << endl;
ofstream file(filename.c_str());
// draw grid first
Box plot_area = generate_grid(file);
// calculate point scales
int sx = (plot_area.x1 - plot_area.x0) / (double) data.size();
int sy = (plot_area.y1 - plot_area.y0) / (double) data.back();
// calculate column width /2
int half_wid = sx / 4;
// set chart color
set_color(file, CHART_R, CHART_G, CHART_B);
// generate columns
for (unsigned i = 0; i < data.size(); i++)
{
// data point position
int x = plot_area.x0 + (i + 1)*sx;
int y = plot_area.y0 + data[i]*sy;
// column polygon points
int x0 = x - half_wid;
int y0 = y;
int x1 = x + half_wid;
int y1 = y;
int x2 = x1;
int y2 = plot_area.y0;
int x3 = x0;
int y3 = plot_area.y0;
draw_polygon(file, 4, x0, y0, x1, y1, x2, y2, x3, y3);
}
file.close();
}
/**
* Generate the point chart commands
*/
void generate_point_chart(vector data)
{
string filename = "point.txt";
cout << "Saving chart in " << filename << "..." << endl;
ofstream file(filename.c_str());
// draw grid first
Box plot_area = generate_grid(file);
// calculate point scales
int sx = (plot_area.x1 - plot_area.x0) / (double) data.size();
int sy = (plot_area.y1 - plot_area.y0) / (double) data.back();
// set chart color
set_color(file, CHART_R, CHART_G, CHART_B);
// generate points
for (unsigned i = 0; i < data.size(); i++)
{
int x = plot_area.x0 + (i + 1)*sx;
int y = plot_area.y0 + data[i]*sy;
draw_point(file, POINT_SIZE, x, y);
}
file.close();
}
/**
* Generate the line chart commands
*/
void generate_line_chart(vector data)
{
string filename = "line.txt";
cout << "Saving chart in " << filename << "..." << endl;
ofstream file(filename.c_str());
// draw grid first
Box plot_area = generate_grid(file);
// calculate point scales
int sx = (plot_area.x1 - plot_area.x0) / (double) data.size();
int sy = (plot_area.y1 - plot_area.y0) / (double) data.back();
// set chart color
set_color(file, CHART_R, CHART_G, CHART_B);
// generate points
for (unsigned i = 0; i < data.size() - 1; i++)
{
int x0 = plot_area.x0 + (i + 1)*sx;
int y0 = plot_area.y0 + data[i]*sy;
int x1 = plot_area.x0 + (i + 2)*sx;
int y1 = plot_area.y0 + data[i + 1]*sy;
draw_line(file, LINE_THICK, x0, y0, x1, y1);
}
file.close();
}