From 3d02d4fac697220301be2a9adad53800739de330 Mon Sep 17 00:00:00 2001
From: Bruce Toll <btoll@dhsus.com>
Date: Wed, 14 Oct 2015 14:06:53 -0400
Subject: [PATCH] Chart with tooltip, layout, and initial zoom.

This is a modified version of ChartsExample.C that adds tooltips, and
places the sine function example in a layout. This drives a JS bug in
some versions of Wt.

In addition, the example sets an initial zoom on the sine function
example and replaces the curve with points. This makes it easier to
verify that the tool tips are correct even in the presence of an initial
zoom.
---
 examples/charts/ChartsExample.C | 42 +++++++++++++++++++++++++++++------------
 1 file changed, 30 insertions(+), 12 deletions(-)

diff --git a/examples/charts/ChartsExample.C b/examples/charts/ChartsExample.C
index 97e3045..f65be7e 100644
--- a/examples/charts/ChartsExample.C
+++ b/examples/charts/ChartsExample.C
@@ -20,6 +20,7 @@
 
 #include <Wt/WBorderLayout>
 #include <Wt/WFitLayout>
+#include <Wt/WVBoxLayout>
 
 #include <Wt/WStandardItem>
 #include <Wt/WTableView>
@@ -59,6 +60,19 @@ namespace {
   };
 
   /*
+   * Handler to update tooltips on model changes
+   */
+  void updateToolTip(WStandardItemModel *model, WStandardItem *item)
+  {
+    WString old_tip = item->toolTip();
+    WString new_tip = asString(model->headerData(item->column())) + ": "
+      + asString(item->data(DisplayRole), "%.f");
+    if (old_tip != new_tip) {
+      item->setToolTip(new_tip);
+    }
+  }
+
+  /*
    * Reads a CSV file as an (editable) standard item model.
    */
   WAbstractItemModel *readCsvFile(const std::string &fname,
@@ -75,18 +89,13 @@ namespace {
 	for (int col = 0; col < model->columnCount(); ++col) {
 	  model->item(row, col)->setFlags(ItemIsSelectable | ItemIsEditable);
 
-	  /*
-	    Example of tool tips (disabled here because they are not updated
-	    when editing data)
- 	   */
-
-	  /*
 	  WString toolTip = asString(model->headerData(col)) + ": "
 	    + asString(model->item(row, col)->data(DisplayRole), "%.f");
 	  model->item(row, col)->setToolTip(toolTip);
-	   */
 	}
 
+      model->itemChanged().connect(boost::bind(updateToolTip, model, _1));
+
       return model;
     } else {
       WString error(WString::tr("error-missing-data"));
@@ -296,12 +305,18 @@ ScatterPlotExample::ScatterPlotExample(WContainerWidget *parent):
 
     model->setData(i, 0, x);
     model->setData(i, 1, sin(x));
+    WString toolTip = asString(model->item(i, 1)->data(DisplayRole), "%.2f");
+    model->item(i, 1)->setToolTip(toolTip);
   }
  
   /*
    * Create the scatter plot.
    */
-  WCartesianChart *chart = new WCartesianChart(this);
+  WContainerWidget *holder = new WContainerWidget(this);
+  holder->setLayout(new WVBoxLayout());
+
+  WCartesianChart *chart = new WCartesianChart();
+  holder->layout()->addWidget(chart);
   chart->setModel(model);        // set the model
   chart->setXSeriesColumn(0);    // set the column that holds the X data
   chart->setLegendEnabled(true); // enable the legend
@@ -309,6 +324,9 @@ ScatterPlotExample::ScatterPlotExample(WContainerWidget *parent):
   chart->setPanEnabled(true);
   chart->setCrosshairEnabled(true);
 
+  chart->axis(XAxis).setZoom(2.0);
+  chart->axis(YAxis).setZoom(2.0);
+
   chart->setBackground(WColor(200,200,200));
 
   chart->setType(ScatterPlot);   // set type to ScatterPlot
@@ -322,14 +340,14 @@ ScatterPlotExample::ScatterPlotExample(WContainerWidget *parent):
   chart->setAutoLayoutEnabled();
 
   // Add the curves
-  WDataSeries s(1, CurveSeries);
+  WDataSeries s(1, PointSeries);
   s.setShadow(WShadow(3, 3, WColor(0, 0, 0, 127), 3));
   chart->addSeries(s);
 
-  chart->resize(800, 300); // WPaintedWidget must be given explicit size
+  holder->resize(800, 300);
 
-  chart->setMargin(10, Top | Bottom);            // add margin vertically
-  chart->setMargin(WLength::Auto, Left | Right); // center horizontally
+  holder->setMargin(10, Top | Bottom);            // add margin vertically
+  holder->setMargin(WLength::Auto, Left | Right); // center horizontally
 
   ChartConfig *config = new ChartConfig(chart, this);
   config->setValueFill(ZeroValueFill);
-- 
1.9.1

