private void createDimensionConfigLegendItem(
      PlotInstance plotInstance,
      DefaultDimensionConfig dimensionConfig,
      Set<PlotDimension> dimensionSet,
      LegendItemCollection legendItemCollection) {
    PlotConfiguration plotConfiguration = plotInstance.getCurrentPlotConfigurationClone();
    DimensionConfigData dimensionConfigData =
        plotInstance.getPlotData().getDimensionConfigData(dimensionConfig);
    if (dimensionConfig.isGrouping()) {
      // create legend entry based on the grouping
      if (dimensionConfig.isNominal()) {
        // create categorical legend --> one item for each category
        createCategoricalLegendItems(
            plotInstance,
            dimensionSet,
            legendItemCollection,
            dimensionConfigData.getDistinctValues());
      } else if (dimensionConfig.isNumerical() || dimensionConfig.isDate()) {
        createDimensionTitleLegendItem(plotInstance, dimensionSet, legendItemCollection);

        // create one continuous legend item
        double minValue = dimensionConfigData.getMinValue();
        double maxValue = dimensionConfigData.getMaxValue();

        LegendItem legendItem =
            createContinuousLegendItem(
                plotInstance,
                dimensionSet,
                minValue,
                maxValue,
                dimensionConfig.isDate() ? dimensionConfig.getDateFormat() : null);
        if (legendItem != null) {
          legendItemCollection.add(legendItem);
        }
      } else {
        throw new RuntimeException(
            "unknown data type during legend creation - this should not happen");
      }
    } else {
      // dimension config not grouping --> create legend item only, if there exists
      // at least one non-aggregated value source (otherwise the dimension config is
      // not used at all in the plot and thus we also don't need a legend item for it).
      boolean createLegend = false;
      for (ValueSource valueSource : plotConfiguration.getAllValueSources()) {
        if (!valueSource.isUsingDomainGrouping()) {
          createLegend = true;
          break;
        }
      }
      if (createLegend) {
        // create legend based on the attribute values on the dimension config
        if (dimensionConfig.isNominal()) {
          // create one legend item for each nominal value
          List<Double> values = dimensionConfigData.getDistinctValues();
          createCategoricalLegendItems(plotInstance, dimensionSet, legendItemCollection, values);
        } else if (dimensionConfig.isNumerical() || dimensionConfig.isDate()) {
          createDimensionTitleLegendItem(plotInstance, dimensionSet, legendItemCollection);

          // create one continuous legend item for the value range
          double minValue = dimensionConfigData.getMinValue();
          double maxValue = dimensionConfigData.getMaxValue();

          LegendItem legendItem =
              createContinuousLegendItem(
                  plotInstance,
                  dimensionSet,
                  minValue,
                  maxValue,
                  dimensionConfig.isDate() ? dimensionConfig.getDateFormat() : null);
          if (legendItem != null) {
            legendItemCollection.add(legendItem);
          }
        } else {
          throw new RuntimeException(
              "unknown data type during legend creation - this should not happen");
        }
      }
    }
  }
  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;
  }