18 #include "KReportItemChart.h"
20 #include "KReportRenderObjects.h"
21 #include "KReportUtils.h"
23 #include <KDChartBarDiagram>
24 #include <KDChartThreeDBarAttributes>
25 #include <KDChartLineDiagram>
26 #include <KDChartThreeDLineAttributes>
27 #include <KDChartPieDiagram>
28 #include <KDChartThreeDPieAttributes>
29 #include <KDChartLegend>
30 #include <KDChartCartesianAxis>
31 #include <KDChartChart>
32 #include <KDChartBackgroundAttributes>
33 #include <KDChartAbstractDiagram>
34 #include <KDChartAbstractCoordinatePlane>
35 #include <KDChartPosition>
38 #include <KPropertySet>
40 #include "kreportplugin_debug.h"
41 #include <QFontDatabase>
45 KReportItemChart::KReportItemChart()
46 : m_reportData(nullptr)
51 KReportItemChart::KReportItemChart(
QDomNode *element)
55 m_name->setValue(KReportUtils::readNameAttribute(e));
56 setItemDataSource(e.
attribute(
"report:item-data-source"));
59 m_chartSubType->setValue(e.
attribute(
"report:chart-sub-type").
toInt());
60 m_threeD->setValue(e.
attribute(
"report:three-dimensions"));
62 m_colorScheme->setValue(e.
attribute(
"report:chart-color-scheme"));
63 m_aa->setValue(e.
attribute(
"report:antialiased"));
64 m_xTitle->setValue(e.
attribute(
"report:title-x-axis"));
65 m_yTitle->setValue(e.
attribute(
"report:title-y-axis"));
66 m_backgroundColor->setValue(e.
attribute(
"report:background-color"));
67 m_displayLegend->setValue(e.
attribute(
"report:display-legend"));
68 m_legendPosition->setValue(e.
attribute(
"report:legend-position"));
69 m_legendOrientation->setValue(e.
attribute(
"report:legend-orientation"));
70 m_linkMaster->setValue(e.
attribute(
"report:link-master"));
71 m_linkChild->setValue(e.
attribute(
"report:link-child"));
73 parseReportRect(e, &m_pos, &m_size);
77 KReportItemChart::~KReportItemChart()
82 void KReportItemChart::createProperties()
87 createDataSourceProperty();
92 keys << 1 << 2 << 3 << 4 << 5;
94 strings << tr(
"Bar") << tr(
"Line") << tr(
"Pie") << tr(
"Ring") << tr(
"Polar");
95 KProperty::ListData *typeData =
new KProperty::ListData(keys, strings);
96 m_chartType =
new KProperty(
"chart-type", typeData, 1, tr(
"Chart Type"));
100 keys << 0 << 1 << 2 << 3;
101 strings << tr(
"Normal") << tr(
"Stacked") << tr(
"Percent") << tr(
"Rows");
103 KProperty::ListData *subData =
new KProperty::ListData(keys, strings);
105 m_chartSubType =
new KProperty(
"chart-sub-type", subData, 0, tr(
"Chart Sub Type"));
110 stringkeys <<
"default" <<
"rainbow" <<
"subdued";
111 strings << tr(
"Default") << tr(
"Rainbow") << tr(
"Subdued");
112 m_colorScheme =
new KProperty(
"chart-color-scheme", stringkeys, strings,
"default", tr(
"Color Scheme"));
115 tr(
"3D",
"Three dimensions"));
121 m_displayLegend =
new KProperty(
"display-legend",
true, tr(
"Display Legend"));
125 keys << (int)KDChartEnums::PositionNorth
126 << (
int)KDChartEnums::PositionEast
127 << (int)KDChartEnums::PositionSouth
128 << (
int)KDChartEnums::PositionWest;
129 QStringList names = KDChart::Position::printableNames();
131 strings << names[
pos.toInt()-1];
133 subData =
new KProperty::ListData(keys, strings);
134 m_legendPosition =
new KProperty(
"legend-position", subData, (
int)KDChartEnums::PositionEast, tr(
"Legend Position"));
139 strings << tr(
"Horizontal") << tr(
"Vertical");
140 subData =
new KProperty::ListData(keys, strings);
141 m_legendOrientation =
new KProperty(
"legend-orientation", subData,
Qt::Vertical, tr(
"Legend Orientation"));
144 tr(
"Background Color"));
147 tr(
"Fields from master data source"));
149 tr(
"Fields from child data source"));
151 addDefaultProperties();
152 m_set->addProperty(m_chartType);
153 m_set->addProperty(m_chartSubType);
154 m_set->addProperty(m_font);
155 m_set->addProperty(m_colorScheme);
156 m_set->addProperty(m_threeD);
157 m_set->addProperty(m_aa);
158 m_set->addProperty(m_xTitle);
159 m_set->addProperty(m_yTitle);
160 m_set->addProperty(m_backgroundColor);
161 m_set->addProperty(m_displayLegend);
162 m_set->addProperty(m_legendPosition);
163 m_set->addProperty(m_legendOrientation);
164 m_set->addProperty(m_linkMaster);
165 m_set->addProperty(m_linkChild);
169 setColorScheme(
"default");
172 void KReportItemChart::set3D(
bool td)
174 if (m_chartWidget && m_chartWidget->barDiagram()) {
175 KDChart::BarDiagram *bar = m_chartWidget->barDiagram();
178 KDChart::ThreeDBarAttributes threed = bar->threeDBarAttributes();
179 threed.setEnabled(td);
182 threed.setUseShadowColors(
true);
183 bar->setThreeDBarAttributes(threed);
187 void KReportItemChart::setAA(
bool aa)
189 if (m_chartWidget && m_chartWidget->diagram()) {
190 m_chartWidget->diagram()->setAntiAliasing(aa);
194 void KReportItemChart::setColorScheme(
const QString &cs)
196 if (m_chartWidget && m_chartWidget->diagram()) {
197 if (cs ==
"rainbow") {
198 m_chartWidget->diagram()->useRainbowColors();
199 }
else if (cs ==
"subdued") {
200 m_chartWidget->diagram()->useSubduedColors();
202 m_chartWidget->diagram()->useDefaultColors();
207 void KReportItemChart::setConnection(
const KReportData *c)
213 void KReportItemChart::populateData()
221 delete m_chartWidget;
225 QString src = itemDataSource();
228 KReportData *curs = m_reportData->create(src);
231 foreach(
const QString& field, keys) {
233 curs->addExpression(field, m_links[field],
'=');
236 if (curs && curs->open()) {
237 fn = curs->fieldNames();
239 int cols = fn.
count() - 1;
244 m_chartWidget =
new KDChart::Widget();
245 m_chartWidget->setType((KDChart::Widget::ChartType) m_chartType->value().toInt());
246 m_chartWidget->setSubType((KDChart::Widget::SubType) m_chartSubType->value().toInt());
247 set3D(m_threeD->value().toBool());
248 setAA(m_aa->value().toBool());
249 setColorScheme(m_colorScheme->value().toString());
250 setBackgroundColor(m_backgroundColor->value().value<
QColor>());
254 labels << curs->
value(0).toString();
255 for (
int i = 1; i <= cols; ++i) {
256 data[i - 1] << curs->
value(i).toDouble();
258 }
while (curs->moveNext());
260 for (
int i = 1; i <= cols; ++i) {
261 m_chartWidget->setDataset(i - 1,
data[i - 1], fn[i]);
264 setLegend(m_displayLegend->value().toBool(), fn);
267 setAxis(m_xTitle->value().toString(), m_yTitle->value().toString());
270 if (m_chartWidget->barDiagram() || m_chartWidget->lineDiagram()) {
271 KDChart::AbstractCartesianDiagram *dia =
static_cast<KDChart::AbstractCartesianDiagram*
>(m_chartWidget->diagram());
273 foreach(KDChart::CartesianAxis* axis, dia->axes()) {
274 if (axis->position() == KDChart::CartesianAxis::Bottom) {
275 axis->setLabels(labels);
280 kreportpluginWarning() <<
"Unable to open data set";
285 kreportpluginWarning() <<
"No source set";
288 kreportpluginWarning() <<
"No connection!";
294 return m_linkMaster->
value().toString().split(
',');
303 void KReportItemChart::setAxis(
const QString& xa,
const QString &ya)
305 if (!m_chartWidget) {
308 Q_ASSERT(m_chartWidget);
310 if (m_chartWidget->barDiagram() || m_chartWidget->lineDiagram()) {
311 KDChart::AbstractCartesianDiagram *dia =
static_cast<KDChart::AbstractCartesianDiagram*
>(m_chartWidget->diagram());
312 KDChart::CartesianAxis *xAxis = 0;
313 KDChart::CartesianAxis *yAxis = 0;
316 foreach(KDChart::CartesianAxis* axis, dia->axes()) {
317 if (axis->position() == KDChart::CartesianAxis::Bottom) {
320 if (axis->position() == KDChart::CartesianAxis::Left) {
326 xAxis =
new KDChart::CartesianAxis(
static_cast<KDChart::AbstractCartesianDiagram*
>(m_chartWidget->diagram()));
327 xAxis->setPosition(KDChart::CartesianAxis::Bottom);
332 yAxis =
new KDChart::CartesianAxis(
static_cast<KDChart::AbstractCartesianDiagram*
>(m_chartWidget->diagram()));
333 yAxis->setPosition(KDChart::CartesianAxis::Left);
337 xAxis->setTitleText(xa);
338 yAxis->setTitleText(ya);
342 void KReportItemChart::setBackgroundColor(
const QColor&)
345 if (!m_chartWidget) {
348 KDChart::Chart *cht = m_chartWidget->diagram()->coordinatePlane()->parent();
350 KDChart::BackgroundAttributes ba;
353 ba.setBrush(m_backgroundColor->value().value<
QColor>());
354 cht->setBackgroundAttributes(ba);
357 void KReportItemChart::setLegend(
bool le,
const QStringList &legends)
361 if (le && ! legends.
isEmpty()) {
362 m_chartWidget->addLegend(KDChart::Position((KDChartEnums::PositionValue)m_legendPosition->value().toInt()));
363 m_chartWidget->legend()->setOrientation((
Qt::Orientation) m_legendOrientation->value().toInt());
364 m_chartWidget->legend()->setTitleText(
"Legend");
365 for (
int i = 1; i < legends.
count(); ++i) {
366 m_chartWidget->legend()->setText(i - 1, legends[i]);
369 m_chartWidget->legend()->setShowLines(
true);
371 if (m_chartWidget->legend()) {
372 m_chartWidget->takeLegend(m_chartWidget->legend());
379 QString KReportItemChart::typeName()
const
385 KReportData *
data, KReportScriptHandler *script)
392 for (
int i = 0; i < masters.
size(); ++i) {
393 if (!masters[i].simplified().isEmpty()) {
394 setLinkData(masters[i],
data->
value(masters[i]));
400 widget()->setFixedSize(m_size.toScene().toSize());
404 widget()->diagram()->coordinatePlane()->parent()->paint(&p,
QRect(
QPoint(0, 0), m_size.toScene().toSize()));
407 QSizeF size = m_size.toScene();
411 pic->setPosition(
pos);
413 if (page) page->addPrimitive(pic);
417 p2->setPosition(m_pos.toPoint());
419 section->addPrimitive(p2);