qt -chart控件设计器可拖拉

qt -chart控件设计器可拖拉

一、演示效果

二、安装过程

三、核心程序

cpp 复制代码
#include <QtGui>
#include <QColor>

#include <cstdlib>
#include <cassert>
#include <numeric>

#include <chartwork/PieChart.h>
#include <chartwork/Design.h>
#include <chartwork/exceptions/OutOfRangeException.h>
#include <chartwork/exceptions/EmptyException.h>
#include <chartwork/exceptions/UnknownItemException.h>

namespace chartwork
{


//
// PieChart
//


PieChart::PieChart(QWidget *parent)
:	Chart(parent),
	m_fontMetrics(m_font),
	m_showPercentage(true),
	m_precision(1),
	m_labels(std::shared_ptr<QStringList>(new QStringList({"A", "B", "C", "D"}))),
	m_key(m_labels, m_colors)
{
	setWindowTitle(tr("Pie Chart"));
	resize(400, 300);
	setMinimumSize(200, 150);

	generateRandomValues();

	m_title.setText("Pie Chart");

	update();
}



const QStringList &PieChart::labels() const
{
	return *m_labels;
}



void PieChart::setLabels(const QStringList &labels)
{
	*m_labels = labels;

	m_key.updateSize();

	generateRandomValues();
	update();
}



bool PieChart::showKey() const
{
	return m_key.isVisible();
}



void PieChart::setShowKey(bool showKey)
{
	m_key.setVisible(showKey);
	update();
}



double PieChart::keyScale() const
{
	return m_key.scale();
}



void PieChart::setKeyScale(double keyScale)
{
	m_key.setScale(keyScale);
	update();
}



bool PieChart::showPercentage() const
{
	return m_showPercentage;
}



void PieChart::setShowPercentage(bool showPercentage)
{
	m_showPercentage = showPercentage;

	update();
}



int PieChart::precision() const
{
	return m_precision;
}



void PieChart::setPrecision(int precision)
{
	m_precision = std::max(0, precision);

	update();
}



void PieChart::resetValues()
{
	m_values.clear();
	m_values.resize(m_labels->size(), 0.0);
}



void PieChart::generateRandomValues()
{
	resetValues();

	std::srand(0);

	for (int i = 0; i < m_labels->size(); i++)
		m_values[i] = std::rand() / (double)RAND_MAX * 100;
}



double PieChart::diameter(int remainingWidth, int remainingHeight) const
{
	return std::max(0.0, std::min((double)remainingWidth, (double)remainingHeight));
}



void PieChart::paintEvent(QPaintEvent *)
{
	if (m_labels->empty())
		return;

	if (m_values.size() < (size_t)m_labels->size())
		return;

	m_title.setWidth(width());
	m_title.moveTopLeft(QPoint(0, design::spacing - m_title.rect().height() / 4));

	int remainingWidth = width() - m_key.rect().width() - 3 * design::spacing;

	if (!m_key.isVisible())
		remainingWidth += design::spacing;

	const int remainingHeight = height() - m_title.rect().height() - 2 * design::spacing;

	m_diameter = diameter(remainingWidth, remainingHeight);

	m_chartRect.setSize(QSize(m_diameter, m_diameter));

	const int outerSpacing = (remainingWidth - m_diameter) / 2;

	m_chartRect.moveLeft(design::spacing + outerSpacing);
	m_chartRect.moveTop(design::spacing + m_title.rect().height() + (remainingHeight - m_diameter) / 2);

	m_key.moveTopRight(QPoint(m_chartRect.left() + m_chartRect.width() + design::spacing + m_key.rect().width(), design::spacing + m_title.rect().height() + (remainingHeight - m_key.rect().height()) / 2));

	QPainter painter(this);

	m_background.paint(painter);
	m_title.paint(painter);
	paint(painter);
	m_key.paint(painter);
}



void PieChart::paint(QPainter &painter)
{
	const double sumOfValues = std::accumulate(m_values.begin(),
		m_values.end(), 0.0, [](double sum, double v){return sum + std::max(0.0, v);});

	painter.setRenderHint(QPainter::Antialiasing);

	double currentAngle = 0;

	for (size_t i = 0; i < m_values.size(); i++)
	{
		const double angleValue = m_values[i] / sumOfValues * 360 * 16;

		if (angleValue <= 0.0)
			continue;

		const QColor &color = (*m_colors)[i % m_colors->size()];
		painter.setBrush(color);
		painter.drawPie(m_chartRect, std::round(-currentAngle + 90 * 16 - angleValue), std::round(angleValue));
		currentAngle += angleValue;
	}

	painter.setRenderHint(QPainter::Antialiasing,false);

	if (!m_showPercentage)
		return;

	currentAngle = 0;

	for (size_t i = 0; i < m_values.size(); i++)
	{
		const double angleValue = m_values[i] / sumOfValues * 2.0 * M_PI;

		if (angleValue <= 0.0)
			continue;

		const double midAngle = -currentAngle + M_PI_2 - angleValue / 2.0f;

		painter.setPen(Qt::SolidLine);
		painter.setFont(m_font);

		const QColor &color = (*m_colors)[i % m_colors->size()];
		if (color.value() < 210)
			painter.setPen(design::superlightgray);

		const QString percentageText = QString("%1").arg(m_values[i] / sumOfValues * 100.0, 0, 'f', m_precision) + QString(" %");

		const QPoint textCenter = m_chartRect.center() + QPoint(m_diameter / 3 * std::cos(midAngle), -m_diameter / 3 * std::sin(midAngle));
		const double textWidth = m_fontMetrics.width(percentageText) + 24;
		const double textHeight = m_fontMetrics.height() + 24;

		painter.drawText(QRect(textCenter.x() - textWidth / 2,
							   textCenter.y() - textHeight / 2,
							   textWidth,
							   textHeight),
							   percentageText, QTextOption(Qt::AlignCenter));
		painter.setPen(Qt::NoPen);

		currentAngle += angleValue;
	}
}



void PieChart::setValue(int slice, double value)
{
	handleValueUpdate();

	if (m_values.empty())
		throw exceptions::EmptyException("PieChart", objectName(), "labels");

	if (slice < 0 || (size_t)slice >= m_values.size())
		throw exceptions::OutOfRangeException("PieChart", objectName(), "slice", slice, 0, (int)m_values.size() - 1);

	m_values[slice] = value;
}



void PieChart::setValue(const QString &label, double value)
{
	if (m_values.empty())
		throw exceptions::EmptyException("PieChart", objectName(), "labels");

	const int sliceIndex = m_labels->indexOf(label);

	if (sliceIndex < 0)
		throw exceptions::UnknownItemException("PieChart", objectName(), "label", label, *m_labels);

	setValue(sliceIndex, value);
}



void PieChart::setAllValues(double value)
{
	handleValueUpdate();

	m_values.clear();
	m_values.resize(m_labels->size(), value);
}



double PieChart::value(int slice) const
{
	if (m_values.empty())
		throw exceptions::EmptyException("PieChart", objectName(), "labels");

	if (slice < 0 || (size_t)slice >= m_values.size())
		throw exceptions::OutOfRangeException("PieChart", objectName(), "slice", slice, 0, (int)m_values.size() - 1);

	return m_values[slice];
}



double PieChart::value(const QString &label) const
{
	if (m_values.empty())
		throw exceptions::EmptyException("PieChart", objectName(), "labels");

	const int sliceIndex = m_labels->indexOf(label);

	if (sliceIndex < 0)
		throw exceptions::UnknownItemException("PieChart", objectName(), "label", label, *m_labels);

	return value(sliceIndex);
}



}

四、程序链接

https://download.csdn.net/download/u013083044/88803860

相关推荐
Ajiang28247353041 小时前
对于C++中stack和queue的认识以及priority_queue的模拟实现
开发语言·c++
幽兰的天空1 小时前
Python 中的模式匹配:深入了解 match 语句
开发语言·python
Theodore_10224 小时前
4 设计模式原则之接口隔离原则
java·开发语言·设计模式·java-ee·接口隔离原则·javaee
----云烟----6 小时前
QT中QString类的各种使用
开发语言·qt
lsx2024066 小时前
SQL SELECT 语句:基础与进阶应用
开发语言
开心工作室_kaic7 小时前
ssm161基于web的资源共享平台的共享与开发+jsp(论文+源码)_kaic
java·开发语言·前端
向宇it7 小时前
【unity小技巧】unity 什么是反射?反射的作用?反射的使用场景?反射的缺点?常用的反射操作?反射常见示例
开发语言·游戏·unity·c#·游戏引擎
武子康7 小时前
Java-06 深入浅出 MyBatis - 一对一模型 SqlMapConfig 与 Mapper 详细讲解测试
java·开发语言·数据仓库·sql·mybatis·springboot·springcloud
转世成为计算机大神7 小时前
易考八股文之Java中的设计模式?
java·开发语言·设计模式
宅小海8 小时前
scala String
大数据·开发语言·scala