Simple Histogram Widget for wxWidgets

When working on Open Yahtzee 1.10 (or what ever I’ll call the version after 1.9), I’ve written a simple histogram widget to be part of the new statistics dialog. I should emphasize the simple part, this widget was mean to display a simple histogram without requiring any special bloated ploting libraries. It doesn’t support all the fancy stuff, just plain histogram.

I’ve figured that a simple pie plot would better serve Open Yahtzee’s needs, so unfortunately this code will not be released as part of the program. While the code is not perfect, it’s functional and serves a good example of a custom widget. So I’ve felt pity letting it fall into oblivion in Open Yahtzee’s SVN repository, and I’ve thought it might come handy to someone else (or at least for me) if it will be easily accessible.

simple_histogram

The widget is made up of two files: simple_histogram.h and simple_histogram.cpp. The code uses stl::vector to pass the plotting data and depenpends on BOOST_FOREACH (but it can easily be removed).

simple_histogram.h:

/***************************************************************************
 *   Copyright (C) 2009 by Guy Rutenberg   *
 *   http://www.guyrutenberg.com/contact-me   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <wx/wx.h>
#include <vector>

#ifndef SIMPLE_HISTOGRAM_INC
#define SIMPLE_HISTOGRAM_INC

class SimpleHistogram : public wxPanel {
public:
	SimpleHistogram (wxWindow* parent, wxWindowID id,
		 const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize,
		 long style = wxNO_BORDER, const wxString& name = wxPanelNameStr);
	void SetData(std::vector<double> d);
	void OnPaint(wxPaintEvent& event);
	void OnResize(wxSizeEvent& event);
private:
	std::vector<double> data;
	double data_total;
};

#endif

simple_histogram.cpp:

/***************************************************************************
 *   Copyright (C) 2009 by Guy Rutenberg   *
 *   http://www.guyrutenberg.com/contact-me   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "simple_histogram.h"
#include <boost/foreach.hpp>
#include <memory>
using namespace std;

SimpleHistogram::SimpleHistogram(wxWindow* parent, wxWindowID id,
				const wxPoint& pos, const wxSize& size,
				long style, const wxString& name)
				: wxPanel(parent, id, pos, size, style, name)
{
	Connect(this->GetId(), wxEVT_PAINT, wxPaintEventHandler(SimpleHistogram::OnPaint));
	Connect(this->GetId(), wxEVT_SIZE, wxSizeEventHandler(SimpleHistogram::OnResize));
	
}

void SimpleHistogram::OnPaint(wxPaintEvent& event)
{
	wxPaintDC pdc(this);
	auto_ptr<wxGraphicsContext> dc(wxGraphicsContext::Create(pdc));
	
	dc->SetBrush(*wxBLUE_BRUSH);
	int width, height;
	GetClientSize(&width, &height);

	const double column_width = static_cast<double>(width) / data.size();

	int i = 0;
	double item_x, item_y, item_ratio, item_height;
	BOOST_FOREACH(double d, data) {
		item_x = i * column_width;
		item_ratio = data_total ? d/data_total : 0;
		item_height = item_ratio * height;
		item_y = height-item_height;
		dc->DrawRectangle(item_x, item_y, column_width, item_height);
		i++;
	}
}

void SimpleHistogram::OnResize(wxSizeEvent& event)
{
	Refresh();
	event.Skip();
}

void SimpleHistogram::SetData(vector<double> d)
{
	data.assign(d.begin(), d.end());
	data_total = 0;
	BOOST_FOREACH(double tmp, data) {
		data_total += tmp;
	}
}

UPDATE 2009-08-17: Removed commented out function – DoGetBestSize().

2 thoughts on “Simple Histogram Widget for wxWidgets”

  1. @tim, Thanks for reporting. I’ll re-check the code as soon as I’ll have time for it (and maybe even add a usage example).

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.