@@ -170,6 +170,25 @@ SizeResponsiveChartView* Chart::createChartView(QChart* chart, int minimumHeight
170
170
}
171
171
172
172
173
+ /* *
174
+ * Creates and initializes a QBarSeries object.
175
+ *
176
+ * Initialization entails adding the series to the given chart and attaching the given axes.
177
+ *
178
+ * @param chart The chart to display in the chart view.
179
+ * @param xAxis The chart's x-axis.
180
+ * @param yAxis The chart's y-axis.
181
+ * @return An initialized QHorizontalBarSeries object, of which the caller takes ownership.
182
+ */
183
+ QBarSeries* Chart::createBarSeries (QChart* chart, QAbstractAxis* xAxis, QAbstractAxis* yAxis)
184
+ {
185
+ QBarSeries* series = new QBarSeries ();
186
+ chart->addSeries (series);
187
+ series->attachAxis (xAxis);
188
+ series->attachAxis (yAxis);
189
+ return series;
190
+ }
191
+
173
192
/* *
174
193
* Creates and initializes a QHorizontalBarSeries object.
175
194
*
@@ -310,31 +329,136 @@ void Chart::resetAxis(QValueAxis* axis)
310
329
311
330
312
331
/* *
313
- * Creates a YearChart .
332
+ * Creates a YearBarChart .
314
333
*
315
- * @param chartTitle The title of the chart, to be displayed above it.
316
- * @param yAxisTitle The label text for the y-axis.
317
- * @param bufferXAxisRange The fraction of the x-axis range to add as buffer.
334
+ * @param chartTitle The title of the chart, to be displayed above it.
335
+ * @param yAxisTitle The label text for the y-axis.
318
336
*/
319
- YearChart::YearChart (const QString& chartTitle, const QString& yAxisTitle, bool bufferXAxisRange) :
337
+ YearBarChart::YearBarChart (const QString& chartTitle, const QString& yAxisTitle) :
338
+ Chart(chartTitle),
339
+ yAxisTitle(yAxisTitle),
340
+ xAxis (nullptr ),
341
+ yAxis (nullptr ),
342
+ barSeries (nullptr ),
343
+ barSet (nullptr )
344
+ {
345
+ YearBarChart::setup ();
346
+ YearBarChart::reset ();
347
+ }
348
+
349
+ /* *
350
+ * Destroys the YearBarChart.
351
+ */
352
+ YearBarChart::~YearBarChart ()
353
+ {
354
+ // xAxis is deleted by chart
355
+ // yAxis is deleted by chart
356
+ // barSeries is deleted by chart
357
+ // barSet is deleted by barSeries
358
+ }
359
+
360
+
361
+
362
+ /* *
363
+ * Performs the setup for the YearBarChart during/after construction.
364
+ *
365
+ * Not to be called more than once (will cause memory leaks).
366
+ */
367
+ void YearBarChart::setup ()
368
+ {
369
+ chart = createChart (chartTitle);
370
+ // xAxis = createBarCategoryXAxis(chart, Qt::AlignBottom);
371
+ xAxis = createValueXAxis (chart);
372
+ yAxis = createValueYAxis (chart, yAxisTitle, Qt::AlignLeft);
373
+ barSeries = createBarSeries (chart, xAxis, yAxis);
374
+ barSet = createBarSet (QString (), barSeries);
375
+ chartView = createChartView (chart);
376
+
377
+ connect (chartView, &SizeResponsiveChartView::wasResized, this , &YearBarChart::updateView);
378
+ }
379
+
380
+ /* *
381
+ * Removes all data from the chart.
382
+ */
383
+ void YearBarChart::reset ()
384
+ {
385
+ barSet->remove (0 , barSet->count ());
386
+ hasData = false ;
387
+ minYear = 0 ;
388
+ maxYear = 0 ;
389
+ maxY = 0 ;
390
+ resetAxis (yAxis);
391
+ }
392
+
393
+ /* *
394
+ * Replaces the displayed data and stores range information for future view updates.
395
+ *
396
+ * Performs a view update before replacing the data.
397
+ *
398
+ * @param histogramData A list of data points to display in the chart. The length of the list must match the number of categories.
399
+ */
400
+ void YearBarChart::updateData (const QList<qreal>& newData, int minYear, int maxYear, qreal maxY)
401
+ {
402
+ if (newData.isEmpty ()) {
403
+ reset ();
404
+ return ;
405
+ }
406
+ assert (newData.size () == maxYear - minYear + 1 );
407
+ assert (maxY >= 0 );
408
+
409
+ QStringList years = QStringList ();
410
+ for (int year = minYear; year <= maxYear; year++) {
411
+ years.append (QString::number (year));
412
+ }
413
+
414
+ barSet->remove (0 , barSet->count ());
415
+ barSet->append (QList<qreal>(minYear, 0 ));
416
+ barSet->append (newData);
417
+
418
+ this ->minYear = minYear;
419
+ this ->maxYear = maxYear;
420
+ this ->maxY = maxY;
421
+ hasData = true ;
422
+ updateView ();
423
+ }
424
+
425
+ /* *
426
+ * Updates the chart layout, e.g. tick spacing, without changing the displayed data.
427
+ */
428
+ void YearBarChart::updateView ()
429
+ {
430
+ if (!hasData) return ;
431
+ adjustAxis (xAxis, minYear, maxYear, chart->plotArea ().width (), rangeBufferFactorX);
432
+ adjustAxis (yAxis, 0 , maxY, chart->plotArea ().height (), rangeBufferFactorY);
433
+ }
434
+
435
+
436
+
437
+
438
+
439
+ /* *
440
+ * Creates a TimeScatterChart.
441
+ *
442
+ * @param chartTitle The title of the chart, to be displayed above it.
443
+ * @param yAxisTitle The label text for the y-axis.
444
+ */
445
+ TimeScatterChart::TimeScatterChart (const QString& chartTitle, const QString& yAxisTitle) :
320
446
Chart(chartTitle),
321
447
yAxisTitle(yAxisTitle),
322
- bufferXAxisRange(bufferXAxisRange),
323
448
xAxis (nullptr ),
324
449
yAxis (nullptr ),
325
450
minYear(0 ),
326
451
maxYear(0 ),
327
- minY(0 ),
328
452
maxY(0 )
329
453
{
330
- YearChart ::setup ();
331
- YearChart ::reset ();
454
+ TimeScatterChart ::setup ();
455
+ TimeScatterChart ::reset ();
332
456
}
333
457
334
458
/* *
335
- * Destroys the YearChart .
459
+ * Destroys the TimeScatterChart .
336
460
*/
337
- YearChart ::~YearChart ()
461
+ TimeScatterChart ::~TimeScatterChart ()
338
462
{
339
463
// xAxis is deleted by chart
340
464
// yAxis is deleted by chart
@@ -343,11 +467,11 @@ YearChart::~YearChart()
343
467
344
468
345
469
/* *
346
- * Performs the setup for the YearChart during/after construction.
470
+ * Performs the setup for the TimeScatterChart during/after construction.
347
471
*
348
472
* Not to be called more than once (will cause memory leaks).
349
473
*/
350
- void YearChart ::setup ()
474
+ void TimeScatterChart ::setup ()
351
475
{
352
476
chart = createChart (chartTitle);
353
477
xAxis = createValueXAxis (chart);
@@ -358,20 +482,19 @@ void YearChart::setup()
358
482
chartView->setInteractive (true );
359
483
chartView->setRubberBand (QChartView::RectangleRubberBand);
360
484
361
- connect (chartView, &SizeResponsiveChartView::wasResized, this , &YearChart ::updateView);
362
- connect (chartView, &SizeResponsiveChartView::receivedDoubleClick, this , &YearChart ::resetZoom);
485
+ connect (chartView, &SizeResponsiveChartView::wasResized, this , &TimeScatterChart ::updateView);
486
+ connect (chartView, &SizeResponsiveChartView::receivedDoubleClick, this , &TimeScatterChart ::resetZoom);
363
487
}
364
488
365
489
/* *
366
490
* Removes all data from the chart.
367
491
*/
368
- void YearChart ::reset ()
492
+ void TimeScatterChart ::reset ()
369
493
{
370
494
chart->removeAllSeries ();
371
495
hasData = false ;
372
496
this ->minYear = 0 ;
373
497
this ->maxYear = 0 ;
374
- this ->minY = 0 ;
375
498
this ->maxY = 0 ;
376
499
resetAxis (xAxis);
377
500
resetAxis (yAxis);
@@ -389,7 +512,7 @@ void YearChart::reset()
389
512
* @param minY The minimum y value among all given data series.
390
513
* @param maxY The maximum y value among all given data series.
391
514
*/
392
- void YearChart ::updateData (const QList<QXYSeries*>& newSeries, qreal minYear, qreal maxYear, qreal minY , qreal maxY)
515
+ void TimeScatterChart ::updateData (const QList<QXYSeries*>& newSeries, qreal minYear, qreal maxYear, qreal maxY)
393
516
{
394
517
bool noData = true ;
395
518
for (QXYSeries* const series : newSeries) {
@@ -403,7 +526,7 @@ void YearChart::updateData(const QList<QXYSeries*>& newSeries, qreal minYear, qr
403
526
return ;
404
527
}
405
528
assert (minYear <= maxYear);
406
- assert (minY <= maxY );
529
+ assert (maxY >= 0 );
407
530
408
531
if (maxYear - minYear < 1 ) {
409
532
qreal buffer = 0.5 * (1 - (maxYear - minYear));
@@ -413,7 +536,6 @@ void YearChart::updateData(const QList<QXYSeries*>& newSeries, qreal minYear, qr
413
536
414
537
this ->minYear = minYear;
415
538
this ->maxYear = maxYear;
416
- this ->minY = minY;
417
539
this ->maxY = maxY;
418
540
hasData = true ;
419
541
updateView ();
@@ -430,19 +552,19 @@ void YearChart::updateData(const QList<QXYSeries*>& newSeries, qreal minYear, qr
430
552
/* *
431
553
* Updates the chart layout, e.g. tick spacing, without changing the displayed data.
432
554
*/
433
- void YearChart ::updateView ()
555
+ void TimeScatterChart ::updateView ()
434
556
{
435
557
if (!hasData) return ;
436
- adjustAxis (xAxis, minYear, maxYear, chart->plotArea ().width (), bufferXAxisRange ? rangeBufferFactorX : 0 , true );
437
- adjustAxis (yAxis, minY, maxY, chart->plotArea ().height (), rangeBufferFactorY);
558
+ adjustAxis (xAxis, minYear, maxYear, chart->plotArea ().width (), rangeBufferFactorX, true );
559
+ adjustAxis (yAxis, 0 , maxY, chart->plotArea ().height (), rangeBufferFactorY);
438
560
}
439
561
440
562
/* *
441
563
* Resets zoom level set by user.
442
564
*
443
565
* To be called when user requests to reset the chart view.
444
566
*/
445
- void YearChart ::resetZoom ()
567
+ void TimeScatterChart ::resetZoom ()
446
568
{
447
569
if (!hasData) return ;
448
570
chart->zoomReset ();
0 commit comments