private static void configureXYDifferenceRenderer(
      FormattedXYDifferenceRenderer renderer,
      ValueSource valueSource,
      PlotConfiguration plotConfiguration) {
    renderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator());
    SeriesFormat seriesFormat = valueSource.getSeriesFormat();
    DimensionConfig domainConfig = valueSource.getDomainConfig();
    DimensionConfig colorDimensionConfig =
        plotConfiguration.getDimensionConfig(PlotDimension.COLOR);
    DimensionConfig shapeDimensionConfig =
        plotConfiguration.getDimensionConfig(PlotDimension.SHAPE);

    int seriesCount = 1; // valueSource.getSeriesDataForAllGroupCells().groupCellCount();

    // Loop all series and set series format.
    // Format based on dimension configs will be set later on in initFormatDelegate().
    for (int seriesIdx = 0; seriesIdx < seriesCount; ++seriesIdx) {
      // configure linestyle
      if (seriesFormat.getLineStyle() == LineStyle.NONE) {
      } else {
        renderer.setSeriesStroke(seriesIdx, seriesFormat.getStroke(), false);
      }

      // configure series shape if necessary
      if (!SeriesFormat.calculateIndividualFormatForEachItem(domainConfig, shapeDimensionConfig)) {
        if (seriesFormat.getItemShape() != ItemShape.NONE) {
          renderer.setSeriesShape(seriesIdx, seriesFormat.getItemShape().getShape());
        } else {
        }
      }

      // configure series color if necessary
      if (!SeriesFormat.calculateIndividualFormatForEachItem(domainConfig, colorDimensionConfig)) {
        Color itemColor = seriesFormat.getItemColor();
        Color halfTransparentPaint =
            DataStructureUtils.setColorAlpha(itemColor, itemColor.getAlpha() / 2);

        renderer.setSeriesPaint(0, halfTransparentPaint);
        renderer.setSeriesFillPaint(0, halfTransparentPaint);
        renderer.setPositivePaint(halfTransparentPaint);
        renderer.setNegativePaint(
            new Color(
                255 - itemColor.getRed(),
                255 - itemColor.getGreen(),
                255 - itemColor.getBlue(),
                itemColor.getAlpha() / 2));
      }
      renderer.setSeriesOutlinePaint(seriesIdx, PlotConfiguration.DEFAULT_SERIES_OUTLINE_PAINT);
    }
  }
  /**
   * Creates the headings for the dimension config items ( like "Color (attribute X)" ) and adds it
   * to legendItemCollection.
   */
  private void createDimensionTitleLegendItem(
      PlotInstance plotInstance,
      Set<PlotDimension> dimensionSet,
      LegendItemCollection legendItemCollection) {
    PlotConfiguration plotConfig = plotInstance.getCurrentPlotConfigurationClone();
    StringBuilder titleBuilder = new StringBuilder();
    boolean first = true;
    boolean showDimensionType = plotConfig.getLegendConfiguration().isShowDimensionType();
    if (showDimensionType) {
      for (PlotDimension dimension : dimensionSet) {
        if (!first) {
          titleBuilder.append(", ");
        }
        titleBuilder.append(dimension.getShortName());
        first = false;
      }
    }

    if (showDimensionType) {
      titleBuilder.append(" (");
    }

    // get unique dimension labels:
    Set<String> uniqueDimensionLabels = new HashSet<String>();
    first = true;
    for (PlotDimension dimension : dimensionSet) {
      DefaultDimensionConfig dimensionConfig =
          (DefaultDimensionConfig) plotConfig.getDimensionConfig(dimension);
      String label = dimensionConfig.getLabel();
      if (label == null) {
        label = I18N.getGUILabel("plotter.unnamed_value_label");
      }

      if (!uniqueDimensionLabels.contains(label)) {
        if (!first) {
          titleBuilder.append(", ");
          first = false;
        }
        titleBuilder.append(label);
        uniqueDimensionLabels.add(label);
      }
    }
    if (showDimensionType) {
      titleBuilder.append(")");
    }
    titleBuilder.append(": ");

    legendItemCollection.add(createTitleLegendItem(titleBuilder.toString(), plotConfig));
  }
  public LegendItemCollection getLegendItems(PlotInstance plotInstance) {
    PlotConfiguration plotConfiguration = plotInstance.getCurrentPlotConfigurationClone();
    LegendItemCollection legendItemCollection = new LegendItemCollection();

    // Get list of ValueSources which get an own legend entry.
    // These are those ValueSources, for which useSeriesFormatForDimension() returns true
    // for at least one dimension (not considering X and VALUE).

    // get all values sources
    List<ValueSource> allValueSources = plotConfiguration.getAllValueSources();

    // add the plots heading (if we have any value sources)
    if (!allValueSources.isEmpty()) {
      legendItemCollection.add(
          createTitleLegendItem(
              I18N.getGUILabel("plotter.legend.plots_heading.label") + ":", plotConfiguration));
    }

    // now find those value sources for which we need a legend entry,
    // and also remember which dimensions are shown in the legend entry (color, shape, ...)
    for (ValueSource valueSource : allValueSources) {
      CustomLegendItem legendItem = createValueSourceLegendItem(plotConfiguration, valueSource);
      if (legendItem != null) {
        legendItemCollection.add(legendItem);
      }
    }

    List<Set<PlotDimension>> dimensionsWithLegend =
        findCompatibleDimensions(plotConfiguration, allValueSources);

    // create legend items for DimensionConfigs
    for (Set<PlotDimension> dimensionSet : dimensionsWithLegend) {
      PlotDimension aDimension = dimensionSet.iterator().next();
      DimensionConfig dimensionConfig = plotConfiguration.getDimensionConfig(aDimension);

      createDimensionConfigLegendItem(
          plotInstance,
          (DefaultDimensionConfig) dimensionConfig,
          dimensionSet,
          legendItemCollection);
    }
    return legendItemCollection;
  }
  @Override
  protected void updatePlotConfiguration() {
    // don't do anything if updates are suspended due to batch updating
    if (suspendUpdates) {
      return;
    }

    PlotConfiguration plotConfiguration = plotInstance.getMasterPlotConfiguration();

    // value when "None" is selected
    String noSelection =
        I18N.getMessage(I18N.getGUIBundle(), "gui.plotter.column.empty_selection.label");

    // stop event processing
    boolean plotConfigurationProcessedEvents = plotConfiguration.isProcessingEvents();
    plotConfiguration.setProcessEvents(false);

    // restore crosshairs
    List<AxisParallelLineConfiguration> clonedListOfDomainLines =
        new LinkedList<AxisParallelLineConfiguration>(listOfDomainLines);
    for (AxisParallelLineConfiguration lineConfig : clonedListOfDomainLines) {
      plotConfiguration.getDomainConfigManager().getCrosshairLines().addLine(lineConfig);
    }

    // x axis column selection
    if (!xAxisColumn.equals(noSelection)) {
      plotConfiguration
          .getDimensionConfig(PlotDimension.DOMAIN)
          .setDataTableColumn(
              new DataTableColumn(currentDataTable, currentDataTable.getColumnIndex(xAxisColumn)));
      plotConfiguration.getDimensionConfig(PlotDimension.DOMAIN).setLogarithmic(xAxisLogarithmic);
    } else {
      // remove config
      if (currentRangeAxisConfig != null) {
        currentRangeAxisConfig.removeRangeAxisConfigListener(rangeAxisConfigListener);
        plotConfiguration.removeRangeAxisConfig(currentRangeAxisConfig);
        currentRangeAxisConfig = null;
      }
      plotConfiguration.setProcessEvents(plotConfigurationProcessedEvents);
      return;
    }

    // y axis column selection
    if (!yAxisColumn.equals(noSelection)) {
      RangeAxisConfig newRangeAxisConfig = new RangeAxisConfig(yAxisColumn, plotConfiguration);
      newRangeAxisConfig.addRangeAxisConfigListener(rangeAxisConfigListener);
      ValueSource valueSource;
      valueSource =
          new ValueSource(
              plotConfiguration,
              new DataTableColumn(currentDataTable, currentDataTable.getColumnIndex(yAxisColumn)),
              AggregationFunctionType.count,
              false);
      SeriesFormat sFormat = new SeriesFormat();
      valueSource.setSeriesFormat(sFormat);
      newRangeAxisConfig.addValueSource(valueSource, null);
      newRangeAxisConfig.setLogarithmicAxis(yAxisLogarithmic);

      // remove old config
      if (currentRangeAxisConfig != null) {
        currentRangeAxisConfig.removeRangeAxisConfigListener(rangeAxisConfigListener);
        plotConfiguration.removeRangeAxisConfig(currentRangeAxisConfig);
      }
      currentRangeAxisConfig = newRangeAxisConfig;
      // add new config and restore crosshairs
      List<AxisParallelLineConfiguration> clonedRangeAxisLineList =
          rangeAxisCrosshairLinesMap.get(newRangeAxisConfig.getLabel());
      if (clonedRangeAxisLineList != null) {
        for (AxisParallelLineConfiguration lineConfig : clonedRangeAxisLineList) {
          newRangeAxisConfig.getCrossHairLines().addLine(lineConfig);
        }
      }
      plotConfiguration.addRangeAxisConfig(newRangeAxisConfig);
      // remember the new config so we can remove it later again
    } else {
      // remove config
      if (currentRangeAxisConfig != null) {
        currentRangeAxisConfig.removeRangeAxisConfigListener(rangeAxisConfigListener);
        plotConfiguration.removeRangeAxisConfig(currentRangeAxisConfig);
        currentRangeAxisConfig = null;
      }
    }

    // color column selection
    if (!colorColumn.equals(noSelection)) {
      plotConfiguration.setDimensionConfig(PlotDimension.COLOR, null);
      DefaultDimensionConfig dimConfig =
          new DefaultDimensionConfig(
              plotConfiguration,
              new DataTableColumn(currentDataTable, currentDataTable.getColumnIndex(colorColumn)),
              PlotDimension.COLOR);
      dimConfig.setLogarithmic(colorLogarithmic);
      plotConfiguration.setDimensionConfig(PlotDimension.COLOR, dimConfig);
    } else {
      plotConfiguration.setDimensionConfig(PlotDimension.COLOR, null);
    }

    // general settings
    plotConfiguration.setAxesFont(styleProvider.getAxesFont());
    plotConfiguration.setTitleFont(styleProvider.getTitleFont());
    plotConfiguration.getLegendConfiguration().setLegendFont(styleProvider.getLegendFont());
    plotConfiguration.addColorSchemeAndSetActive(styleProvider.getColorScheme());
    if (styleProvider.isShowLegend()) {
      plotConfiguration.getLegendConfiguration().setLegendPosition(LegendPosition.BOTTOM);
    } else {
      plotConfiguration.getLegendConfiguration().setLegendPosition(LegendPosition.NONE);
    }
    plotConfiguration.setFrameBackgroundColor(
        ColorRGB.convertToColor(styleProvider.getFrameBackgroundColor()));
    plotConfiguration.setPlotBackgroundColor(
        ColorRGB.convertToColor(styleProvider.getPlotBackgroundColor()));
    plotConfiguration.setTitleText(styleProvider.getTitleText());

    // continue event processing
    plotConfiguration.setProcessEvents(plotConfigurationProcessedEvents);
  }
  private void createCategoricalLegendItems(
      PlotInstance plotInstance,
      Set<PlotDimension> dimensionSet,
      LegendItemCollection legendItemCollection,
      Iterable<Double> values) {
    createDimensionTitleLegendItem(plotInstance, dimensionSet, legendItemCollection);

    PlotConfiguration plotConfig = plotInstance.getCurrentPlotConfigurationClone();

    Shape defaultShape = new Ellipse2D.Float(-5f, -5f, 10f, 10f);
    Color defaultOutlineColor = PlotConfiguration.DEFAULT_OUTLINE_COLOR;
    ColorProvider colorProvider = null;
    ShapeProvider shapeProvider = null;
    SizeProvider sizeProvider = null;

    DefaultDimensionConfig dimensionConfig =
        (DefaultDimensionConfig) plotConfig.getDimensionConfig(dimensionSet.iterator().next());
    DimensionConfigData dimensionConfigData =
        plotInstance.getPlotData().getDimensionConfigData(dimensionConfig);
    for (PlotDimension dimension : dimensionSet) {
      if (dimension == PlotDimension.COLOR) {
        colorProvider = dimensionConfigData.getColorProvider();
      } else if (dimension == PlotDimension.SHAPE) {
        shapeProvider = dimensionConfigData.getShapeProvider();
      } else if (dimension == PlotDimension.SIZE) {
        sizeProvider = dimensionConfigData.getSizeProvider();
      }
    }

    // initialize size scale for legend
    ContinuousSizeProvider legendSizeProvider = null;
    if (sizeProvider != null) {
      double minScalingFactor = sizeProvider.getMinScalingFactor();
      double maxScalingFactor = sizeProvider.getMaxScalingFactor();
      double minLegendScalingFactor = MIN_LEGEND_ITEM_SCALING_FACTOR;
      double maxLegendScalingFactor = MAX_LEGEND_ITEM_SCALING_FACTOR;
      if (minScalingFactor > maxScalingFactor) {
        double tmp = minScalingFactor;
        minScalingFactor = maxScalingFactor;
        maxScalingFactor = tmp;
        minLegendScalingFactor = MAX_LEGEND_ITEM_SCALING_FACTOR;
        maxLegendScalingFactor = MIN_LEGEND_ITEM_SCALING_FACTOR;
      }
      legendSizeProvider =
          new ContinuousSizeProvider(
              minScalingFactor,
              maxScalingFactor,
              minLegendScalingFactor,
              maxLegendScalingFactor,
              false);
    }

    for (Double value : values) {
      // configure shape and stroke
      Shape shape = defaultShape;
      BasicStroke outlineStroke;
      Color outlineColor = new Color(0, 0, 0, 0);
      if (shapeProvider != null) {
        shape = shapeProvider.getShapeForCategory(value);
        outlineStroke = DEFAULT_OUTLINE_STROKE;
        outlineColor = defaultOutlineColor;
      } else {
        outlineStroke = new BasicStroke();
        if (colorProvider != null) {
          shape = UNDEFINED_SHAPE;
        } else {
          shape = UNDEFINED_SHAPE_AND_COLOR;
        }
      }

      // configure fill paint
      Paint paint = UNDEFINED_COLOR_PAINT;
      if (colorProvider != null) {
        paint = colorProvider.getColorForValue(value);
      }

      double scalingFactor = 1;
      if (sizeProvider != null) {
        // scale shape according to sizeProvider
        scalingFactor = sizeProvider.getScalingFactorForValue(value);
        // scale shape to fit into legend
        scalingFactor = legendSizeProvider.getScalingFactorForValue(scalingFactor);
        AffineTransform transformation = new AffineTransform();
        transformation.scale(scalingFactor, scalingFactor);
        shape = transformation.createTransformedShape(shape);
      }

      String label = dimensionConfigData.getStringForValue(value);
      if (label == null) {
        label = "";
      }

      CustomLegendItem legendItem =
          new CustomLegendItem(label, null, null, null, shape, paint, outlineStroke, outlineColor);
      legendItemCollection.add(legendItem);
    }
  }
  /**
   * Creates a continuous legend item for one item in dimensionSet, i.e. dimensionSet must be a set
   * containing exactly one value.
   *
   * @param dateFormat format used to format minValue and maxValue as dates, or null if they should
   *     be displayed numerically instead of as dates
   * @throws ChartPlottimeException
   */
  private LegendItem createContinuousLegendItem(
      PlotInstance plotInstance,
      Set<PlotDimension> dimensionSet,
      double minValue,
      double maxValue,
      DateFormat dateFormat) {
    PlotConfiguration plotConfiguration = plotInstance.getCurrentPlotConfigurationClone();
    PlotDimension dimension = dimensionSet.iterator().next();
    DefaultDimensionConfig dimensionConfig =
        (DefaultDimensionConfig) plotConfiguration.getDimensionConfig(dimension);
    DimensionConfigData dimensionConfigData =
        plotInstance.getPlotData().getDimensionConfigData(dimensionConfig);
    // String label = dimensionConfig.getLabel();
    // if(label == null) {
    // label = I18N.getGUILabel("plotter.unnamed_value_label");
    // }
    String label = "";

    if (dimension == PlotDimension.COLOR) {
      ColorProvider colorProvider = dimensionConfigData.getColorProvider();
      if (!colorProvider.supportsNumericalValues()) {
        throw new RuntimeException(
            "Color provider for continuous legend item does not support numerical values.");
      }

      // shape dimensions
      final int width = 50;
      final int height = 10;

      // create item paint

      // first disable logarithmic scale on color provider ( -> linear gradient in legend)
      // ContinuousColorProvider continuousColorProvider = null;
      // if (dimensionConfig.isLogarithmic() && colorProvider instanceof
      // ContinuousColorProvider) {
      // continuousColorProvider = (ContinuousColorProvider)colorProvider;
      // continuousColorProvider.setLogarithmic(false);
      // }

      // calculate gradient
      float fractions[] = new float[width];
      Color colors[] = new Color[width];
      for (int i = 0; i < width; ++i) {

        float fraction = i / (width - 1.0f);
        double fractionValue;
        if (colorProvider instanceof ContinuousColorProvider
            && ((ContinuousColorProvider) colorProvider)
                .isColorMinMaxValueDifferentFromOriginal(
                    ((ContinuousColorProvider) colorProvider).getMinValue(),
                    ((ContinuousColorProvider) colorProvider).getMaxValue())) {
          fractionValue =
              ((ContinuousColorProvider) colorProvider).getMinValue()
                  + fraction
                      * (((ContinuousColorProvider) colorProvider).getMaxValue()
                          - ((ContinuousColorProvider) colorProvider).getMinValue());
        } else {
          fractionValue = minValue + fraction * (maxValue - minValue);
        }
        colors[i] = colorProvider.getColorForValue(fractionValue);
        fractions[i] = fraction;
      }
      LinearGradientPaint shapeFillPaint =
          new LinearGradientPaint(
              new Point(0, 0), new Point(width, 0), fractions, colors, CycleMethod.REPEAT);

      // reset color provider to logarithmic if necessary
      // if (continuousColorProvider != null && dimensionConfig.isLogarithmic()) {
      // continuousColorProvider.setLogarithmic(true);
      // }

      // create item shape
      Rectangle itemShape = new Rectangle(width, height);

      if (colorProvider instanceof ContinuousColorProvider) {
        return createFlankedShapeLegendItem(
            label,
            ((ContinuousColorProvider) colorProvider).getMinValue(),
            ((ContinuousColorProvider) colorProvider).getMaxValue(),
            itemShape,
            shapeFillPaint,
            true,
            dateFormat);
      } else {
        return createFlankedShapeLegendItem(
            label, minValue, maxValue, itemShape, shapeFillPaint, true, dateFormat);
      }
    } else if (dimension == PlotDimension.SHAPE) {
      // shape provider probably never supports numerical values
      return null;
    } else if (dimension == PlotDimension.SIZE) {
      SizeProvider sizeProvider = dimensionConfigData.getSizeProvider();

      if (!sizeProvider.supportsNumericalValues()) {
        throw new RuntimeException(
            "Size provider for continuous legend item does not support numerical values.");
      }

      double minScalingFactor = sizeProvider.getMinScalingFactor();
      double maxScalingFactor = sizeProvider.getMaxScalingFactor();
      ContinuousSizeProvider legendSizeProvider =
          new ContinuousSizeProvider(
              minScalingFactor,
              maxScalingFactor,
              MIN_LEGEND_ITEM_SCALING_FACTOR,
              MAX_LEGEND_ITEM_SCALING_FACTOR,
              false);

      int legendItemCount = 4;
      Area composedShape = new Area();
      Shape originalShape = UNDEFINED_SHAPE;
      if (dimensionSet.contains(PlotDimension.SIZE) && dimensionSet.size() == 1) {
        originalShape = UNDEFINED_SHAPE_AND_COLOR;
      }
      double maxHeight = originalShape.getBounds().getHeight() * MAX_LEGEND_ITEM_SCALING_FACTOR;
      for (int i = 0; i < legendItemCount; ++i) {
        double fraction =
            minScalingFactor
                + ((double) i / legendItemCount * (maxScalingFactor - minScalingFactor));
        double legendScalingFactor = legendSizeProvider.getScalingFactorForValue(fraction);

        double composedWidth = composedShape.getBounds().getWidth();

        AffineTransform t = new AffineTransform();
        t.scale(legendScalingFactor, legendScalingFactor);
        Shape shape = t.createTransformedShape(originalShape);

        t = new AffineTransform();
        double shapeWidth = shape.getBounds().getWidth();
        double shapeHeight = shape.getBounds().getHeight();
        t.translate(composedWidth + shapeWidth * .1, (maxHeight - shapeHeight) / 2.0);
        t.translate(-shape.getBounds().getMinX(), -shape.getBounds().getMinY());
        shape = t.createTransformedShape(shape);
        composedShape.add(new Area(shape));
      }

      return createFlankedShapeLegendItem(
          label, minValue, maxValue, composedShape, UNDEFINED_COLOR_PAINT, false, dateFormat);

    } else {
      throw new RuntimeException(
          "Unsupported dimension. Execution path should never reach this line.");
    }
  }
  private List<Set<PlotDimension>> findCompatibleDimensions(
      PlotConfiguration plotConfiguration, List<ValueSource> allValueSources) {
    Map<PlotDimension, DefaultDimensionConfig> dimensionConfigMap =
        plotConfiguration.getDefaultDimensionConfigs();

    // find all Dimensions for which we create a legend item
    List<Set<PlotDimension>> dimensionsWithLegend = new LinkedList<Set<PlotDimension>>();
    for (Entry<PlotDimension, DefaultDimensionConfig> dimensionEntry :
        dimensionConfigMap.entrySet()) {
      PlotDimension dimension = dimensionEntry.getKey();
      DefaultDimensionConfig dimensionConfig = dimensionEntry.getValue();

      boolean createLegend = false;
      if (dimensionConfig.isGrouping()) {
        createLegend = true;
      } else {
        for (ValueSource valueSource : allValueSources) {
          if (!valueSource.isUsingDomainGrouping()) {
            createLegend = true;
            break;
          }
        }
      }

      if (createLegend) {
        if (!dimensionConfig.isNominal()) {
          Set<PlotDimension> newSet = new HashSet<DimensionConfig.PlotDimension>();
          newSet.add(dimension);
          dimensionsWithLegend.add(newSet);
        } else {
          // iterate over list and find dimensions with compatible properties
          boolean compatibleToSomething = false;
          for (Set<PlotDimension> dimensionSet : dimensionsWithLegend) {
            boolean compatible = true;
            for (PlotDimension comparedDimension : dimensionSet) {
              DefaultDimensionConfig comparedDimensionConfig =
                  (DefaultDimensionConfig) plotConfiguration.getDimensionConfig(comparedDimension);
              if (!comparedDimensionConfig.isNominal()) {
                compatible = false;
                break;
              }
              if (!dimensionConfig
                  .getDataTableColumn()
                  .equals(comparedDimensionConfig.getDataTableColumn())) {
                compatible = false;
                break;
              } else if (comparedDimensionConfig.isGrouping()
                  && comparedDimensionConfig.getGrouping() instanceof DistinctValueGrouping
                  && !dimensionConfig.isGrouping()
                  && dimensionConfig.isNominal()) {
                compatible = true;
              } else if (dimensionConfig.isGrouping()
                  && dimensionConfig.getGrouping() instanceof DistinctValueGrouping
                  && !comparedDimensionConfig.isGrouping()
                  && comparedDimensionConfig.isNominal()) {
                compatible = true;
              } else if (dimensionConfig.isGrouping() != comparedDimensionConfig.isGrouping()) {
                compatible = false;
                break;
              } else if (!dimensionConfig.isGrouping()) {
                compatible = true;
              } else if (dimensionConfig
                  .getGrouping()
                  .equals(comparedDimensionConfig.getGrouping())) {
                compatible = true;
              } else {
                compatible = false;
                break;
              }
            }
            if (compatible) {
              dimensionSet.add(dimension);
              compatibleToSomething = true;
              break;
            }
          }
          if (!compatibleToSomething) {
            Set<PlotDimension> newSet = new HashSet<DimensionConfig.PlotDimension>();
            newSet.add(dimension);
            dimensionsWithLegend.add(newSet);
          }
        }
      }
    }
    return dimensionsWithLegend;
  }