3

最近、各プロジェクトのデータを示す棒グラフを作成する必要があります。次に例を示します。

棒グラフの例

ご覧Categoryのとおり、はプロジェクトの名前であり、そのプロジェクトのSeriesさまざまなタイプのデータです。

ただし、システムはプロジェクト名の一意性を保証しないため、カテゴリとして使用すると問題が発生する可能性があり、プロジェクト名を使用してさまざまなプロジェクトのURLを生成することはできません。一方、カテゴリとして一意のIDを使用すると、プロジェクト名を表示できません。これは私を困難な状況に陥らせました。

だから私の問題は:

JFreeChartでその場でカスタマイズカテゴリラベルを生成する方法はありますか?

CategoryItemLabelGeneratorカテゴリ自体に似ていますが、何か。したがって、一意のIDをカテゴリとして使用できますが、プロジェクト名をグラフで表示します。

4

2 に答える 2

4

答えは、選択したインターフェースCategoryDatasetの実装方法によって異なります。KeyedValues2Dインターフェイスはキーが一意であることを想定しており、デフォルトの実装でDefaultKeyedValues2Dは、キーがComparable不変である必要があります。

一意Stringのインスタンスは一般的な具体的なパラメータタイプですJFreeChartが、一意の制約を強制するものはありません。1つのアプローチは、一意性Stringを実装し、強制するクラスでラップすることです。Comparableは、基盤となる実装を活用class Valueする例です。Double実装ではproject、おそらくソースリレーションの主キーを使用して、あるフォームを別のフォームと区別するための追加の属性が必要になります。toString()名前のフォーマットされた表現を取得するためにオーバーライドできます。

于 2012-06-07T16:44:26.080 に答える
2

This is how I implement UniqueValue

public class UniqueValue implements Comparable<UniqueValue> {

    private final String uniqueId;
    private final String value;

    public UniqueValue(String uniqueId, String value) {
        this.uniqueId = uniqueId;
        this.value = value;
    }

    @Override
    public int compareTo(UniqueValue o) {
        return uniqueId.compareTo(o.uniqueId);
    }

    @Override
    public int hashCode() {
        return uniqueId.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof UniqueValue) {
            return uniqueId.equals(((UniqueValue)obj).uniqueId);
        }
        return false;
    }

    @Override
    public String toString() {
            return value;
        }
    }

You can use this class to deal with the uniqueness issue I mentioned in questions.


UPDATE

However, since JFreeChart use toString() method to create the label for categories. So the toString() implementation in UniqueValue can be pretty weird. So here is another attempt.

First, a generator interface

public interface CategoryLabelGenerator {
    public String generate(Comparable<?> category);
}

then I make a subclass for CategoryAxis

public class CategoryLabelCustomizableCategoryAxis extends CategoryAxis {

    private static final long serialVersionUID = 1L;
    private CategoryLabelGenerator labelGenerator;

    public CategoryLabelCustomizableCategoryAxis(String label) {
        super(label);
    }

    public void setCategoryLabelGenerator(CategoryLabelGenerator generator) {
        this.labelGenerator = generator;
    }

    @Override
    protected TextBlock createLabel(Comparable category, float width, 
        RectangleEdge edge, Graphics2D g2) {
        if (generator == null) {
            return super.createLabel(category, width, edge, g2);
        }
        return TextUtilities.createTextBlock(
            labelGenerator.generate(category),  // generate label for category on the fly
            getTickLabelFont(category), getTickLabelPaint(category), width,
            getMaximumCategoryLabelLines(), new G2TextMeasurer(g2));
    }
}

example :

JFreeChart chart = makeChart();
CategoryPlot plot = chart.getCategoryPlot();

CategoryAxis axis = new CategoryLabelCustomizableCategoryAxis();
axis.setCategoryLabelGenerator(new MyCategoryLabelGenerator());
plot.setDomainAxis(axis);

This is how I customize category label. (At least for chart that use CategoryAxis as domain axis..)

于 2012-06-08T02:35:31.427 に答える