下面列出了javafx.scene.chart.BarChart#setLegendVisible ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public ImageBarChartSample() {
String imageBarChartCss = ImageBarChartSample.class.getResource("ImageBarChart.css").toExternalForm();
BarChart barChart = new BarChart(new CategoryAxis(), new NumberAxis());
barChart.setLegendVisible(false);
barChart.getStylesheets().add(imageBarChartCss);
barChart.getData().add(
new XYChart.Series<String, Integer>("Sales Per Product",
FXCollections.observableArrayList(
new XYChart.Data<String, Integer>("SUV", 120),
new XYChart.Data<String, Integer>("Sedan", 50),
new XYChart.Data<String, Integer>("Truck", 180),
new XYChart.Data<String, Integer>("Van", 20))));
Scene scene = new Scene(barChart, 350, 300);
scene.getStylesheets().add(ImageBarChartSample.class.getResource("ImageBarChart.css").toString());
getChildren().add(barChart);
}
public ImageBarChartSample() {
String imageBarChartCss = ImageBarChartSample.class.getResource("ImageBarChart.css").toExternalForm();
BarChart barChart = new BarChart(new CategoryAxis(), new NumberAxis());
barChart.setLegendVisible(false);
barChart.getStylesheets().add(imageBarChartCss);
barChart.getData().add(
new XYChart.Series<String, Integer>("Sales Per Product",
FXCollections.observableArrayList(
new XYChart.Data<String, Integer>("SUV", 120),
new XYChart.Data<String, Integer>("Sedan", 50),
new XYChart.Data<String, Integer>("Truck", 180),
new XYChart.Data<String, Integer>("Van", 20))));
Scene scene = new Scene(barChart, 350, 300);
scene.getStylesheets().add(ImageBarChartSample.class.getResource("ImageBarChart.css").toString());
getChildren().add(barChart);
}
public AreaBarChart(@Nonnull ModularFeatureListRow row, AtomicDouble progress) {
XYChart.Series data = new XYChart.Series();
int i = 1;
int size = row.getFeatures().size();
for (Entry<RawDataFile, ModularFeature> entry : row.getFeatures().entrySet()) {
Property<Float> areaProperty = entry.getValue().get(AreaType.class);
data.getData().add(
new XYChart.Data("" + i, areaProperty.getValue() == null ? 0f : areaProperty.getValue()));
i++;
if (progress != null)
progress.addAndGet(1.0 / size);
}
final CategoryAxis xAxis = new CategoryAxis();
final NumberAxis yAxis = new NumberAxis();
final BarChart<String, Number> bc = new BarChart<String, Number>(xAxis, yAxis);
bc.setLegendVisible(false);
bc.setMinHeight(100);
bc.setPrefHeight(100);
bc.setMaxHeight(100);
bc.setBarGap(3);
bc.setCategoryGap(3);
bc.setPrefWidth(150);
bc.getData().addAll(data);
this.getChildren().add(bc);
}
protected BarChart<String, Number> createChart() {
final CategoryAxis xAxis = new CategoryAxis();
final NumberAxis yAxis = new NumberAxis(0,50,10);
final BarChart<String,Number> bc = new BarChart<String,Number>(xAxis,yAxis);
bc.setId("barAudioDemo");
bc.setLegendVisible(false);
bc.setAnimated(false);
bc.setBarGap(0);
bc.setCategoryGap(1);
bc.setVerticalGridLinesVisible(false);
// setup chart
bc.setTitle("Live Audio Spectrum Data");
xAxis.setLabel("Frequency Bands");
yAxis.setLabel("Magnitudes");
yAxis.setTickLabelFormatter(new NumberAxis.DefaultFormatter(yAxis,null,"dB"));
// add starting data
XYChart.Series<String,Number> series1 = new XYChart.Series<String,Number>();
series1.setName("Data Series 1");
//noinspection unchecked
series1Data = new XYChart.Data[128];
String[] categories = new String[128];
for (int i=0; i<series1Data.length; i++) {
categories[i] = Integer.toString(i+1);
series1Data[i] = new XYChart.Data<String,Number>(categories[i],50);
series1.getData().add(series1Data[i]);
}
bc.getData().add(series1);
return bc;
}
protected BarChart<String, Number> createChart() {
final CategoryAxis xAxis = new CategoryAxis();
final NumberAxis yAxis = new NumberAxis(0,50,10);
final BarChart<String,Number> bc = new BarChart<String,Number>(xAxis,yAxis);
bc.setId("barAudioDemo");
bc.setLegendVisible(false);
bc.setAnimated(false);
bc.setBarGap(0);
bc.setCategoryGap(1);
bc.setVerticalGridLinesVisible(false);
// setup chart
bc.setTitle("Live Audio Spectrum Data");
xAxis.setLabel("Frequency Bands");
yAxis.setLabel("Magnitudes");
yAxis.setTickLabelFormatter(new NumberAxis.DefaultFormatter(yAxis,null,"dB"));
// add starting data
XYChart.Series<String,Number> series1 = new XYChart.Series<String,Number>();
series1.setName("Data Series 1");
//noinspection unchecked
series1Data = new XYChart.Data[128];
String[] categories = new String[128];
for (int i=0; i<series1Data.length; i++) {
categories[i] = Integer.toString(i+1);
series1Data[i] = new XYChart.Data<String,Number>(categories[i],50);
series1.getData().add(series1Data[i]);
}
bc.getData().add(series1);
return bc;
}
protected BarChart<String, Number> createChart() {
final CategoryAxis xAxis = new CategoryAxis();
final NumberAxis yAxis = new NumberAxis(0,50,10);
final BarChart<String,Number> bc = new BarChart<String,Number>(xAxis,yAxis);
bc.setId("barAudioDemo");
bc.setLegendVisible(false);
bc.setAnimated(false);
bc.setBarGap(0);
bc.setCategoryGap(1);
bc.setVerticalGridLinesVisible(false);
// setup chart
bc.setTitle("Live Audio Spectrum Data");
xAxis.setLabel("Frequency Bands");
yAxis.setLabel("Magnitudes");
yAxis.setTickLabelFormatter(new NumberAxis.DefaultFormatter(yAxis,null,"dB"));
// add starting data
XYChart.Series<String,Number> series1 = new XYChart.Series<String,Number>();
series1.setName("Data Series 1");
//noinspection unchecked
series1Data = new XYChart.Data[128];
String[] categories = new String[128];
for (int i=0; i<series1Data.length; i++) {
categories[i] = Integer.toString(i+1);
series1Data[i] = new XYChart.Data<String,Number>(categories[i],50);
series1.getData().add(series1Data[i]);
}
bc.getData().add(series1);
return bc;
}
public final void initializeRankDistributionChart(){
xAxis = new CategoryAxis();
yAxis = new NumberAxis();
xAxis.setLabel("Rank");
xAxis.setTickLength(5);
yAxis.setTickLabelsVisible(false);
yAxis.setTickMarkVisible(false);
rankDistributionChart = new BarChart(xAxis,yAxis);
rankDistributionChart.setLegendVisible(false);
UIUtils.setSize(rankDistributionChart, Main.columnWidthRIGHT+5, 300);
rankDistributionChart.setCategoryGap(0);
}
private BarChart<String, Number> createBarChart(String categoryLabel) {
// X-Axis category
CategoryAxis xAxis = new CategoryAxis();
xAxis.setLabel(categoryLabel);
// Y-Axis (%)
NumberAxis yAxis = new NumberAxis(0, 100, 10);
yAxis.setLabel(DesignerLocalizer.instance().getLangString("percent"));
yAxis.setAutoRanging(false);
yAxis.setUpperBound(100.0d);
yAxis.setLowerBound(0.0d);
// create bar chart
BarChart<String, Number> chBarChart = new BarChart<>(xAxis, yAxis);
chBarChart.setTitle(chartTitle);
chBarChart.setLegendVisible(false);
chBarChart.setAnimated(false);
chBarChart.getData().add(barChartSeries);
// add the points
double total = totalCount.doubleValue();
if (total > 0.0d) {
int count = 0;
for (ParetoItem paretoItem : paretoItems) {
if (count > TOP_N) {
break;
}
count++;
Float percentage = new Float(paretoItem.getValue().floatValue() / total * 100.0f);
XYChart.Data<String, Number> point = new XYChart.Data<>(paretoItem.getCategory(), percentage);
barChartSeries.getData().add(point);
}
}
// add listener for mouse click on bar
for (Series<String, Number> series : chBarChart.getData()) {
for (XYChart.Data<String, Number> item : series.getData()) {
item.getNode().setOnMouseClicked((MouseEvent event) -> {
onBarChartNodeSelected(item);
});
}
}
return chBarChart;
}
public static void setBarChartColors(BarChart chart, List<String> palette,
boolean showLegend) {
if (chart == null || palette == null) {
return;
}
List<XYChart.Series> seriesList = chart.getData();
if (seriesList == null
|| seriesList.size() > palette.size()) {
return;
}
for (int i = 0; i < seriesList.size(); i++) {
XYChart.Series series = seriesList.get(i);
if (series.getData() == null) {
continue;
}
for (int j = 0; j < series.getData().size(); j++) {
XYChart.Data item = (XYChart.Data) series.getData().get(j);
if (item.getNode() != null) {
String color = palette.get(i);
item.getNode().setStyle("-fx-bar-fill: " + color + ";");
}
}
}
chart.setLegendVisible(showLegend);
if (showLegend) {
Set<Node> legendItems = chart.lookupAll("Label.chart-legend-item");
if (legendItems.isEmpty()) {
return;
}
for (Node legendItem : legendItems) {
Label legendLabel = (Label) legendItem;
Node legend = legendLabel.getGraphic();
if (legend != null) {
for (int i = 0; i < seriesList.size(); i++) {
if (seriesList.get(i).getName().equals(legendLabel.getText())) {
legend.setStyle("-fx-background-color: " + palette.get(i));
break;
}
}
}
}
}
}