#include <QApplication>
#include "threedscatterselect.h"
#include <sstream>
#include <algorithm>
#include <assert.h>
using namespace std;
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
app.setStyleSheet("* {font-family:arial;font-size:11px}");
ThreeDScatterSelect demo;
demo.show();
return app.exec();
}
ThreeDScatterSelect::ThreeDScatterSelect(QWidget *parent) :
QDialog(parent), random(3)
{
xData = random.getSeries(20, 100, -10, 10);
yData = random.getSeries(20, 100, -10, 10);
zData = random.getSeries(20, 100, -10, 10);
selected.resize(xData.len);
setWindowTitle("Track Line with Data Labels");
// Create the QChartViewer and draw the chart
m_ChartViewer = new QChartViewer(this);
drawChart(m_ChartViewer);
// Set the window to be of the same size as the chart
setFixedSize(m_ChartViewer->width(), m_ChartViewer->height());
// Set up the mouseMovePlotArea handler for drawing the track cursor
connect(m_ChartViewer, SIGNAL(clicked(QMouseEvent*)), SLOT(onChartClicked(QMouseEvent*)));
}
ThreeDScatterSelect::~ThreeDScatterSelect()
{
delete m_ChartViewer->getChart();
}
//
// Draw the chart and display it in the given viewer
//
void ThreeDScatterSelect::drawChart(QChartViewer *viewer)
{
// Create a ThreeDScatterChart object of size 720 x 520 pixels
ThreeDScatterChart *c = new ThreeDScatterChart(720, 520);
// Add a title to the chart using 20 points Times New Roman Italic font
c->addTitle("3D Scatter Chart (2) ", "timesi.ttf", 20);
// Set the center of the plot region at (350, 240), and set width x depth x height to 360 x 360
// x 270 pixels
c->setPlotRegion(350, 240, 360, 360, 270);
// Set the elevation and rotation angles to 15 and 30 degrees
c->setViewAngle(15, 30);
// Add a scatter group to the chart using 13 pixels glass sphere symbols, in which the color
// depends on the z value of the symbol
ThreeDScatterGroup *g = c->addScatterGroup(xData, yData, zData, "", Chart::GlassSphere2Shape,
13, Chart::SameAsMainColor);
// Add grey (888888) drop lines to the symbols
g->setDropLine(0x888888);
// Add a color axis (the legend) in which the left center is anchored at (645, 220). Set the
// length to 200 pixels and the labels on the right side. Use smooth gradient coloring.
c->setColorAxis(645, 220, Chart::Left, 200, Chart::Right)->setColorGradient();
// Set the x, y and z axis titles using 10 points Arial Bold font
c->xAxis()->setTitle("X-Axis Place Holder", "arialbd.ttf", 10);
c->yAxis()->setTitle("Y-Axis Place Holder", "arialbd.ttf", 10);
c->zAxis()->setTitle("Z-Axis Place Holder", "arialbd.ttf", 10);
// Output the chart
viewer->setChart(c);
// Include tool tip for the chart
viewer->setImageMap(
c->getHTMLImageMap("clickable", "index={dataItem}", ""));
}
void ThreeDScatterSelect::drawDynamicLayer(QChartViewer *viewer)
{
ThreeDScatterChart *c = (ThreeDScatterChart *)viewer->getChart();
DrawArea *d = c->initDynamicLayer();
for (int i = 0; i < (int)selected.size(); ++i)
{
if (selected[i])
{
int x = c->getXCoor(xData[i], yData[i], zData[i]);
int y = c->getYCoor(xData[i], yData[i], zData[i]);
d->rect(x - 8, y - 8, x + 8, y + 8, 0x000000, Chart::Transparent, Chart::flatBorder(2));
}
}
viewer->updateDisplay();
}
//
// User clicks on the QChartViewer
//
void ThreeDScatterSelect::onChartClicked(QMouseEvent * /*event*/)
{
QChartViewer *viewer = (QChartViewer *)QObject::sender();
ImageMapHandler *handler = viewer->getImageMapHandler();
if (0 != handler)
{
// Query the ImageMapHandler to see if the mouse is on a clickable hot spot. We
// consider the hot spot as clickable if its href ("path") parameter is not empty.
const char *path = handler->getValue("path");
if ((0 != path) && (0 != *path))
{
const char *strIndex = handler->getValue("index");
if (0 != strIndex)
{
int index = atoi(strIndex);
selected[index] = !selected[index];
drawDynamicLayer(viewer);
}
}
}
}
|