private void updateHistogramGraph(double min, double max) {
    xAxis.clear();
    currentMaxMin.max = max;
    currentMaxMin.min = min;
    histogramFunc.setMinMax(min, max);
    histogramFunc.setIgnoreOutliers(true);
    // sanity check if there is actually a histogram live
    List<? extends Dataset> newHistogram = histogramFunc.value(data);
    Dataset histogram = newHistogram.get(0);
    if (histograms != null && histograms.size() > 1) {
      histograms.set(histograms.size() - 1, histogram);
    } else {
      histograms = new ArrayList<Dataset>();
      histograms.add(histogram);
    }

    // Some logging to check on the histograms size
    logger.debug("number of histograms stored is {}", histograms.size());
    if (histograms.size() > 10) {
      logger.warn(
          "Number of stored histograms is over expected levels, now at {}", histograms.size());
    }

    xAxis.setValues(
        DatasetUtils.linSpace(min, max, Math.max(1, histogram.getSize() + 1), Dataset.FLOAT64));
    histogramPlotter.setXAxisValues(xAxis, 1);
    generateHistogramUpdate();
    updateChannelGraphs();
    histogramPlotter.refresh(true);
  }
 public void setGraphLines(boolean checked) {
   if (histogramPlotter != null) {
     Plot1DGraphTable table = histogramPlotter.getColourTable();
     for (int i = 0; i < 4; i++) table.getLegendEntry(i).setVisible(checked);
   }
   histogramPlotter.updateAllAppearance();
   histogramPlotter.refresh(false);
 }
 protected void buildGradientImage() {
   GlobalColourMaps.mappingFunctions.get(0);
   AbstractMapFunction redFunc =
       GlobalColourMaps.mappingFunctions.get(cmbRedColour.getSelectionIndex());
   AbstractMapFunction greenFunc =
       GlobalColourMaps.mappingFunctions.get(cmbGreenColour.getSelectionIndex());
   AbstractMapFunction blueFunc =
       GlobalColourMaps.mappingFunctions.get(cmbBlueColour.getSelectionIndex());
   AbstractMapFunction alphaFunc =
       GlobalColourMaps.mappingFunctions.get(cmbAlpha.getSelectionIndex());
   boolean negRed = btnRedInverse.getSelection();
   boolean negGreen = btnGreenInverse.getSelection();
   boolean negBlue = btnBlueInverse.getSelection();
   boolean negAlpha = btnAlphaInverse.getSelection();
   histogramPlotter.applyColourCast(
       redFunc, greenFunc, blueFunc, alphaFunc, negRed, negGreen, negBlue, negAlpha, 0, 256);
   histogramPlotter.refresh(false);
 }
  @Override
  public void widgetSelected(SelectionEvent e) {
    if (e.getSource().equals(cmbRedColour)
        || e.getSource().equals(cmbGreenColour)
        || e.getSource().equals(cmbBlueColour)
        || e.getSource().equals(cmbAlpha)
        || e.getSource().equals(btnRedInverse)
        || e.getSource().equals(btnGreenInverse)
        || e.getSource().equals(btnBlueInverse)
        || e.getSource().equals(btnAlphaInverse)) {
      curAlphaSelect = cmbAlpha.getSelectionIndex();
      curRedSelect = cmbRedColour.getSelectionIndex();
      curGreenSelect = cmbGreenColour.getSelectionIndex();
      curBlueSelect = cmbBlueColour.getSelectionIndex();
      curAlphaInverse = btnAlphaInverse.getSelection();
      curRedInverse = btnRedInverse.getSelection();
      curGreenInverse = btnGreenInverse.getSelection();
      curBlueInverse = btnBlueInverse.getSelection();
      if (hasData()) {
        if (e.getSource().equals(cmbRedColour)) handleChange(0);
        else if (e.getSource().equals(cmbGreenColour)) handleChange(1);
        else if (e.getSource().equals(cmbBlueColour)) handleChange(2);
        else if (e.getSource().equals(cmbAlpha)) handleChange(3);

        updateChannelGraphs();
        generateHistogramUpdate();
      }
      if (e.getSource().equals(cmbGreenColour)
          || e.getSource().equals(cmbRedColour)
          || e.getSource().equals(btnRedInverse)
          || e.getSource().equals(btnGreenInverse)
          || e.getSource().equals(btnBlueInverse)
          || e.getSource().equals(btnAlphaInverse)) histogramPlotter.refresh(true);
      buildGradientImage();
    } else if (e.getSource().equals(cmbColourMap)) {
      colourSettings.put(getPartName(), cmbColourMap.getSelectionIndex());
      buildSelection();
      handleChange(0);
      handleChange(1);
      handleChange(2);
      if (hasData()) {
        updateChannelGraphs();
        generateHistogramUpdate();
      }
      histogramPlotter.refresh(true);
      buildGradientImage();
      if (cmbColourMap.getItem(cmbColourMap.getSelectionIndex()).equals("Mark's special")) {
        spnMinValue.setSelection((int) (specialMinThreshold * 100));
        spnMaxValue.setSelection((int) (specialMaxThreshold * 100));
        threshPanel.setVisible(true);
        ((GridData) threshPanel.getLayoutData()).exclude = false;
      } else {
        threshPanel.setVisible(false);
        ((GridData) threshPanel.getLayoutData()).exclude = true;
      }
      container.layout();
    } else if (e.getSource().equals(spnMinValue) || e.getSource().equals(spnMaxValue)) {
      SpecialExposureFunction redFunc =
          (SpecialExposureFunction)
              GlobalColourMaps.mappingFunctions.get(cmbRedColour.getSelectionIndex());
      SpecialExposureFunction greenFunc =
          (SpecialExposureFunction)
              GlobalColourMaps.mappingFunctions.get(cmbGreenColour.getSelectionIndex());
      SpecialExposureFunction blueFunc =
          (SpecialExposureFunction)
              GlobalColourMaps.mappingFunctions.get(cmbBlueColour.getSelectionIndex());
      redFunc.setThresholds(spnMinValue.getSelection() / 100.0, spnMaxValue.getSelection() / 100.0);
      greenFunc.setThresholds(
          spnMinValue.getSelection() / 100.0, spnMaxValue.getSelection() / 100.0);
      blueFunc.setThresholds(
          spnMinValue.getSelection() / 100.0, spnMaxValue.getSelection() / 100.0);
      specialMaxThreshold = spnMaxValue.getSelection() / 100.0;
      specialMinThreshold = spnMinValue.getSelection() / 100.0;
      updateChannelGraphs();
      generateHistogramUpdate();
      histogramPlotter.refresh(true);
    } else if (e.getSource().equals(spnRangeStart.getControl())
        || e.getSource().equals(spnRangeStop.getControl())) {
      final double min = spnRangeStart.getDouble();
      final double max = spnRangeStop.getDouble();
      updateHistogramGraph(min, max);
    } else if (e.getSource().equals(sldMinValue) || e.getSource().equals(sldMaxValue)) {
      final double min =
          spnRangeStart.getMinimum()
              + spnRangeStart.getRange()
                  * (sldMinValue.getSelection() / (double) sldMinValue.getMaximum());
      final double max =
          spnRangeStart.getMinimum()
              + spnRangeStart.getRange()
                  * (sldMaxValue.getSelection() / (double) sldMaxValue.getMaximum());
      spnRangeStart.setDouble(min);
      spnRangeStop.setDouble(max);
      if (hasData()) updateHistogramGraph(min, max);
    } else if (e.getSource().equals(btnExpertMode)) {
      expertGroup.setVisible(btnExpertMode.getSelection());
      ((GridData) expertGroup.getLayoutData()).exclude = !btnExpertMode.getSelection();
      container.layout();
    }
  }