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 3 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
17 #include "bar-chart.h"
18 #include "ucompose.hpp"
19 #include <cairomm/context.h>
21 BarChart::BarChart(std::list<unsigned int> bars, std::list<Gdk::Color> colours,
22 unsigned int max_value)
26 d_max_value = max_value;
33 bool BarChart::on_expose_event(GdkEventExpose* event)
35 // This is where we draw on the window
36 Glib::RefPtr<Gdk::Window> window = get_window();
39 Gtk::Allocation allocation = get_allocation();
40 const int width = allocation.get_width();
41 const int height = allocation.get_height();
43 // coordinates for the center of the window
49 Cairo::RefPtr<Cairo::Context> cr = window->create_cairo_context();
51 // clip to the area indicated by the expose event so that we only redraw
52 // the portion of the window that needs to be redrawn
53 cr->rectangle(event->area.x, event->area.y,
54 event->area.width, event->area.height);
56 cr->set_source_rgb (0.8, 0.8, 0.8);
57 cr->set_line_width(1000.0);
59 cr->line_to(width, height);
61 cr->set_line_width((double)lw);
64 std::list<unsigned int>::iterator bit = d_bars.begin();
67 for (; bit != d_bars.end(); bit++)
98 else if (max < 100000)
104 unsigned int voffs = 15;
105 unsigned int hoffs = 15;
108 bit = d_bars.begin();
109 std::list<Gdk::Color>::iterator cit = d_colours.begin();
111 for (; bit != d_bars.end(), cit != d_colours.end(); bit++, cit++, i+=(lw+d))
113 cr->move_to(hoffs, i + lw + voffs);
114 double red = (double)(*cit).get_red() /65535.0;
115 double green = (double)(*cit).get_green() /65535.0;
116 double blue = (double)(*cit).get_blue() /65535.0;
117 cr->set_source_rgb(red, green, blue);
118 cr->line_to(((float) *bit / (float)max) * (width - hoffs) + hoffs,
122 cr->set_source_rgb (0.2, 0.2, 0.2);
124 cr->set_line_width((double)lw);
126 // draw the line across the bottom
127 cr->move_to(hoffs, i + lw + voffs);
128 cr->line_to(((float)max / (float)max) * (width - (hoffs * 2)),
132 //now the three ticks
133 cr->move_to(hoffs + 1, i + lw + voffs + 1);
134 cr->line_to(hoffs + 1, i + lw + voffs + (voffs / 2) + 1);
136 cr->move_to(((float)0.25 * ((float)width - (hoffs * 2.0))) + (hoffs / 2), i + lw + voffs + 1);
137 cr->line_to(((float)0.25 * ((float)width - (hoffs * 2.0))) + (hoffs / 2),
138 i + lw + voffs + (voffs / 4) + 1);
140 cr->move_to(((float)0.5 * ((float)width - (hoffs * 2.0))) + (hoffs / 2), i + lw + voffs + 1);
141 cr->line_to(((float)0.5 * ((float)width - (hoffs * 2.0))) + (hoffs / 2),
142 i + lw + voffs + (voffs / 2) + 1);
144 cr->move_to(((float)0.75 * ((float)width - (hoffs * 2.0))) + (hoffs / 2), i + lw + voffs + 1);
145 cr->line_to(((float)0.75 * ((float)width - (hoffs * 2.0))) + (hoffs / 2),
146 i + lw + voffs + (voffs / 4) + 1);
148 cr->move_to(((float)1.0 * ((float)width - ((float)hoffs * 2.0))) - 1, i + lw + voffs + 1);
149 cr->line_to(((float)1.0 * ((float)width - ((float)hoffs * 2.0))) - 1,
150 i + lw + voffs + (voffs / 2) + 1);
153 // now draw the labels
154 Glib::RefPtr<Pango::Layout> layout = Glib::wrap (pango_cairo_create_layout (cr->cobj ()));
155 std::string text_font = "Sans 8";
156 Pango::FontDescription font_desc (text_font);
157 layout->set_font_description (font_desc);
158 layout->set_text("0");
160 layout->get_pixel_size (w, h);
161 cr->move_to(hoffs + 1 - (w / 2), i + lw + voffs + (voffs / 2) + 1);
162 cr->set_source_rgb (0.0, 0.0, 0.0);
163 cr->set_operator (Cairo::OPERATOR_ATOP);
164 pango_cairo_show_layout (cr->cobj (), layout->gobj ());
166 layout->set_text(String::ucompose("%1", max / 2));
167 layout->get_pixel_size (w, h);
168 cr->move_to(((float)0.5 * ((float)width - (hoffs * 2.0))) + (hoffs / 2) -
169 ( w/2), i + lw + voffs + (voffs / 2) + 1);
170 cr->set_source_rgb (0.0, 0.0, 0.0);
171 cr->set_operator (Cairo::OPERATOR_ATOP);
172 pango_cairo_show_layout (cr->cobj (), layout->gobj ());
174 layout->set_text(String::ucompose("%1", max));
175 layout->get_pixel_size (w, h);
176 cr->move_to(((float)1.0 * ((float)width - ((float)hoffs * 2.0))) - 1 -
177 (w / 2), i + lw + voffs + (voffs / 2) + 1);
178 cr->set_source_rgb (0.0, 0.0, 0.0);
179 cr->set_operator (Cairo::OPERATOR_ATOP);
180 pango_cairo_show_layout (cr->cobj (), layout->gobj ());