1
// Copyright (C) 2007, 2008 Ben Asselstine
3
// This program is free software; you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation; either version 2 of the License, or
6
// (at your option) any later version.
8
// This program is distributed in the hope that it will be useful,
9
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
// GNU Library General Public License for more details.
13
// You should have received a copy of the GNU General Public License
14
// along with this program; if not, write to the Free Software
15
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
1
18
#include "line-chart.h"
2
19
#include <cairomm/context.h>
4
LineChart::LineChart(std::list<std::list<unsigned int> > lines, std::list<Gdk::Color> colours, unsigned int max_height_value)
21
LineChart::LineChart(std::list<std::list<unsigned int> > lines,
22
std::list<Gdk::Color> colours,
23
unsigned int max_height_value,
24
std::string x_axis_description,
25
std::string y_axis_description)
7
28
d_colours = colours;
8
29
d_max_height_value = max_height_value;
30
d_x_axis_description = x_axis_description;
31
d_y_axis_description = y_axis_description;
80
116
cr->set_source_rgb(red, green, blue);
81
117
std::list<unsigned int>::iterator it = (*line).begin();
82
118
unsigned int turn = 1;
83
cr->move_to(origin_x, origin_y);
119
cr->move_to(origin_x + hoffs, origin_y - voffs);
84
120
for (; it != (*line).end(); it++, turn++)
86
cr->line_to(((float)turn / (float)max_turn) * width,
87
(height - (((float)*it / (float)d_max_height_value) * height)));
122
cr->line_to((((float)turn / (float)max_turn) *
123
(width - (hoffs * 2))) + hoffs,
124
height - (voffs * 2) -
125
(((float)*it / (float)d_max_height_value) *
126
(height - (voffs * 2.0))) + voffs);
131
//draw horizontal axis
132
cr->set_source_rgb(0.3, 0.3, 0.3);
133
cr->move_to(origin_x + hoffs, origin_y - voffs);
134
cr->line_to((width - (hoffs * 2)) + hoffs, origin_y - voffs);
137
//draw ticks on the horizontal axis
138
cr->set_source_rgb(0.3, 0.3, 0.3);
139
cr->move_to(origin_x + hoffs, origin_y - voffs + 1);
140
cr->line_to(origin_x + hoffs, origin_y - voffs + (voffs / 4) + 1);
143
cr->move_to((width - (hoffs * 2)) + hoffs - 1, origin_y - voffs + 1);
144
cr->line_to((width - (hoffs * 2)) + hoffs - 1,
145
origin_y - voffs + (voffs / 4) + 1);
149
cr->set_source_rgb(0.3, 0.3, 0.3);
150
cr->move_to(origin_x + hoffs, origin_y - voffs);
151
cr->line_to(origin_x + hoffs, voffs);
154
//draw ticks on the vertical axis
155
cr->move_to(origin_x + hoffs - 1, origin_y - voffs);
156
cr->line_to(origin_x + hoffs - (hoffs / 4) - 1, origin_y - voffs);
158
cr->move_to(origin_x + hoffs - 1, voffs + 1);
159
cr->line_to(origin_x + hoffs - (hoffs / 4) - 1, voffs + 1);
162
// draw the labels on the horizontal axis
163
layout->set_font_description (font_desc);
164
layout->set_text("0");
165
layout->get_pixel_size (w, h);
166
cr->move_to(hoffs - (w / 2), origin_y - voffs + (voffs / 4));
167
cr->set_source_rgb (0.0, 0.0, 0.0);
168
cr->set_operator (Cairo::OPERATOR_ATOP);
169
pango_cairo_show_layout (cr->cobj (), layout->gobj ());
171
snprintf (buf, sizeof (buf), "%d", max_turn);
172
layout->set_text(buf);
173
layout->get_pixel_size (w, h);
174
cr->move_to((width - (hoffs * 2)) + hoffs - (w / 2),
175
origin_y - voffs + (voffs / 4) + 1);
176
cr->set_source_rgb (0.0, 0.0, 0.0);
177
cr->set_operator (Cairo::OPERATOR_ATOP);
178
pango_cairo_show_layout (cr->cobj (), layout->gobj ());
180
layout->set_text(d_y_axis_description);
181
layout->get_pixel_size (w, h);
182
cr->move_to((width / 2 - (hoffs * 1)) + hoffs - (w / 2),
183
origin_y - voffs + (voffs / 2) + 1);
184
cr->set_source_rgb (0.0, 0.0, 0.0);
185
cr->set_operator (Cairo::OPERATOR_ATOP);
186
pango_cairo_show_layout (cr->cobj (), layout->gobj ());
188
// draw the labels on the vertical axis
189
layout->set_font_description (font_desc);
190
layout->set_text("0");
191
layout->get_pixel_size (w, h);
192
cr->move_to(origin_x + hoffs - (hoffs / 4) - 1 - w, origin_y - voffs - (h/2));
193
cr->set_source_rgb (0.0, 0.0, 0.0);
194
cr->set_operator (Cairo::OPERATOR_ATOP);
195
pango_cairo_show_layout (cr->cobj (), layout->gobj ());
197
snprintf (buf, sizeof (buf), "%d", d_max_height_value);
198
layout->set_text(buf);
199
layout->get_pixel_size (w, h);
200
cr->move_to(origin_x + hoffs - (hoffs / 4) - 1 - w, voffs + 1 - (h/2));
201
cr->set_source_rgb (0.0, 0.0, 0.0);
202
cr->set_operator (Cairo::OPERATOR_ATOP);
203
pango_cairo_show_layout (cr->cobj (), layout->gobj ());
205
PangoContext *context;
206
PangoCairoFontMap *fontmap;
207
fontmap = (PangoCairoFontMap *) pango_cairo_font_map_get_default ();
208
context = pango_cairo_font_map_create_context (fontmap);
209
pango_context_set_base_gravity(context, PANGO_GRAVITY_EAST);
210
pango_context_set_gravity_hint(context, PANGO_GRAVITY_HINT_STRONG);
211
layout->context_changed();
213
layout->set_text(d_x_axis_description);
214
layout->get_pixel_size (w, h);
215
cr->move_to(0, height - (voffs * 2) -
216
(0.50 * (height - (voffs * 2.0))) + voffs + (w / 2));
217
cr->rotate(-90 / (180.0 / G_PI));
218
pango_cairo_show_layout (cr->cobj (), layout->gobj ());
220
//draw the indicator line
92
221
if (d_x_indicator > -1 && d_x_indicator <= max_turn)
94
223
//draw a line at turn x
95
224
cr->set_source_rgb(0.0, 0.0, 0.0);
96
cr->move_to(((float)d_x_indicator/ (float)max_turn) * width, 0);
97
cr->line_to(((float)d_x_indicator/ (float)max_turn) * width, height);
225
cr->move_to((((float)d_x_indicator/ (float)max_turn) * (width - (hoffs * 2))) + hoffs, voffs);
226
cr->line_to((((float)d_x_indicator/ (float)max_turn) * (width - (hoffs * 2))) + hoffs, height - voffs);
102
//FIXME: add a horizontal axis, put some ticks on it, and label it.
103
//FIXME: add a vertical axis, put some ticks on it, and label it.