在饼图中显示其他值 [英] Display additional values in Pie chart

查看:201
本文介绍了在饼图中显示其他值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有饼图数据的这个例子:

  import javafx.application.Application; 
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.chart.PieChart;
import javafx.scene.chart.PieChart.Data;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.Label;
import javafx.scene.control.MenuItem;
import javafx.scene.effect.Glow;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

公共类MainApp扩展应用
{

阶段阶段;

PieChart图表;
ObservableList< Data> pieChartData = FXCollections.observableArrayList();
标签标题;

@Override
public void start(阶段阶段)
{

this.stage = stage;

setUserAgentStylesheet(STYLESHEET_CASPIAN);
场景场景=新场景(新组());
stage.setTitle(进口水果);
stage.setWidth(500);
stage.setHeight(500);

chart = new PieChart(pieChartData);
chart.setTitle(进口水果);

//添加一些数据
addPieChartData(Grapefruit,13);
addPieChartData(Oranges,25);
addPieChartData(Plums,10);
addPieChartData(Pears,22);
addPieChartData(苹果,30);

//更新饼图
最终任务任务的一些任务;
task = new任务< Void>()
{
@Override
protected Void call()抛出异常
{
int max = 50;
int l = 0;
for(int i = 1; i< = max; i ++)
{

updatePieChartData(Grapefruit,l ++);
updatePieChartData(Oranges,l ++);

Thread.sleep(600);
}
返回null;
}
};

new Thread(task).start();

((Group)scene.getRoot())。getChildren()。addAll(chart,caption);
stage.setScene(场景);
stage.show();
}

public void addPieChartData(String name,double value)
{
pieChartData.add(new Data(name,value));

caption = new Label();
caption.setTextFill(Color.DARKORANGE);
caption.setStyle( - fx-font:24 arial;);

for(final data data:chart.getData())
{

Node node = data.getNode();

node.addEventHandler(MouseEvent.MOUSE_MOVED,new EventHandler< MouseEvent>()
{
@Override
public void handle(MouseEvent e)
{
caption.setTranslateX(e.getSceneX()+ 15);
caption.setTranslateY(e.getSceneY());
caption.setText(String.valueOf(data.getPieValue()) +%);
caption.setVisible(true);
node.setEffect(new Glow());
// String styleString = - fx-border-color:white; -fx-border-width:1; -fx-border-style:dashed;;
//node.setStyle(styleString);
}
});

node.addEventHandler(MouseEvent.MOUSE_EXITED,new EventHandler< MouseEvent>()
{
@Override
public void handle(MouseEvent e)
{
caption.setVisible(false);
node.setEffect(null);
//node.setStyle();
}
});

final MenuItem resizeItem = new MenuItem(Resize);
resizeItem.setOnAction(new EventHandler< ActionEvent>()
{
@Override
public void handle(ActionEvent event)
{
System.out。 println(请求调整大小);
}
});

final MenuItem aboutItem = new MenuItem(About);
aboutItem.setOnAction(new EventHandler< ActionEvent>()
{
@Override
public void handle(ActionEvent event)
{
System.out。 println(关于请求);
}
});

final MenuItem changeColorItem = new MenuItem(Change Color);
changeColorItem.setOnAction(new EventHandler< ActionEvent>()
{
@Override
public void handle(ActionEvent event)
{
System.out。 println(更改颜色项目请求);

}
});

final ContextMenu menu = new ContextMenu(resizeItem,aboutItem,changeColorItem);

node.setOnMouseClicked(new EventHandler< MouseEvent>()
{
@Override
public void handle(MouseEvent event)
{
if(MouseButton.SECONDARY.equals(event.getButton()))
{
menu.show(stage,event.getScreenX(),event.getScreenY());
}
}
});

}
}

//如果名称匹配
则更新现有数据对象public void updatePieChartData(String name,double value)
{
for(Data d:pieChartData)
{
if(d.getName()。equals(name))
{
d.setPieValue(value);
返回;
}
}
addPieChartData(name,value);
}

public static void main(String [] args)
{
launch(args);
}
}

在折线图中是否有类似于tickLabelFormatter的方法在饼图名称中显示其他值?



我想显示每个Pie切片的名称和编号。

解决方案

如果您更改了值(例如示例代码中)和/或想要避免在图表图例中显示值,则可以修改文本标签而不是更改数据名称。 / p>

每个数据项都有一个文本节点,其中包含显示为饼图标签的字符串。在通过覆盖 PieChart#layoutChartChildren 绘制图表子项之前,可以更新此字符串:

  chart = new PieChart(pieChartData){
@Override
protected void layoutChartChildren(double top,double left,double contentWidth,double contentHeight){
if(getLabelsVisible()){
getData()。forEach(d - > {
可选<节点> opTextNode = chart.lookupAll(。chart-pie-label)。stream()。filter(n - > n instanceof Text&&((Text)n).getText()。contains(d.getName()))。findAny();
if(opTextNode.isPresent()){
((文本)opTextNode.get())。setText(d.getName()++ d.getPieValue()+Tons);
}
});
}
super.layoutChartChildren(top,left,contentWidth,contentHeight);
}
};


I have this example of Pie chart data:

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.chart.PieChart;
import javafx.scene.chart.PieChart.Data;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.Label;
import javafx.scene.control.MenuItem;
import javafx.scene.effect.Glow;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class MainApp extends Application
{

    Stage stage;

    PieChart chart;
    ObservableList<Data> pieChartData = FXCollections.observableArrayList();
    Label caption;

    @Override
    public void start(Stage stage)
    {

        this.stage = stage;

        setUserAgentStylesheet(STYLESHEET_CASPIAN);
        Scene scene = new Scene(new Group());
        stage.setTitle("Imported Fruits");
        stage.setWidth(500);
        stage.setHeight(500);

        chart = new PieChart(pieChartData);
        chart.setTitle("Imported Fruits");

        // Add some data
        addPieChartData("Grapefruit", 13);
        addPieChartData("Oranges", 25);
        addPieChartData("Plums", 10);
        addPieChartData("Pears", 22);
        addPieChartData("Apples", 30);

        // Some task which updates the Pie Chart
        final Task task;
        task = new Task<Void>()
        {
            @Override
            protected Void call() throws Exception
            {
                int max = 50;
                int l = 0;
                for (int i = 1; i <= max; i++)
                {

                    updatePieChartData("Grapefruit", l++);
                    updatePieChartData("Oranges", l++);

                    Thread.sleep(600);
                }
                return null;
            }
        };

        new Thread(task).start();

        ((Group) scene.getRoot()).getChildren().addAll(chart, caption);
        stage.setScene(scene);
        stage.show();
    }

    public void addPieChartData(String name, double value)
    {
        pieChartData.add(new Data(name, value));

        caption = new Label();
        caption.setTextFill(Color.DARKORANGE);
        caption.setStyle("-fx-font: 24 arial;");

        for (final Data data : chart.getData())
        {

            Node node = data.getNode();

            node.addEventHandler(MouseEvent.MOUSE_MOVED, new EventHandler<MouseEvent>()
            {
                @Override
                public void handle(MouseEvent e)
                {
                    caption.setTranslateX(e.getSceneX() + 15);
                    caption.setTranslateY(e.getSceneY());
                    caption.setText(String.valueOf(data.getPieValue()) + "%");
                    caption.setVisible(true);
                    node.setEffect(new Glow());
                    //String styleString = "-fx-border-color: white; -fx-border-width: 1; -fx-border-style: dashed;";
                    //node.setStyle(styleString);
                }
            });

            node.addEventHandler(MouseEvent.MOUSE_EXITED, new EventHandler<MouseEvent>()
            {
                @Override
                public void handle(MouseEvent e)
                {
                    caption.setVisible(false);
                    node.setEffect(null);
                    //node.setStyle("");
                }
            });

            final MenuItem resizeItem = new MenuItem("Resize");
            resizeItem.setOnAction(new EventHandler<ActionEvent>()
            {
                @Override
                public void handle(ActionEvent event)
                {
                    System.out.println("Resize requested");
                }
            });

            final MenuItem aboutItem = new MenuItem("About");
            aboutItem.setOnAction(new EventHandler<ActionEvent>()
            {
                @Override
                public void handle(ActionEvent event)
                {
                    System.out.println("About requested");
                }
            });

            final MenuItem changeColorItem = new MenuItem("Change Color");
            changeColorItem.setOnAction(new EventHandler<ActionEvent>()
            {
                @Override
                public void handle(ActionEvent event)
                {
                    System.out.println("change Color Item requested");

                }
            });

            final ContextMenu menu = new ContextMenu(resizeItem, aboutItem, changeColorItem);

            node.setOnMouseClicked(new EventHandler<MouseEvent>()
            {
                @Override
                public void handle(MouseEvent event)
                {
                    if (MouseButton.SECONDARY.equals(event.getButton()))
                    {
                        menu.show(stage, event.getScreenX(), event.getScreenY());
                    }
                }
            });

        }
    }

    // updates existing Data-Object if name matches
    public void updatePieChartData(String name, double value)
    {
        for (Data d : pieChartData)
        {
            if (d.getName().equals(name))
            {
                d.setPieValue(value);
                return;
            }
        }
        addPieChartData(name, value);
    }

    public static void main(String[] args)
    {
        launch(args);
    }
}

Is there any way similar to tickLabelFormatter in Line chart to display additional values in Pie chart names?

I want to display each Pie slice with names and number.

解决方案

If you have changing values (like in the example code) and/or want to avoid displaying the values in the chart legend, you could modify the text label instead of changing the data names.

Each data item has a text node that holds the string which is shown as the pie chart label. This string can be updated before the chart children are drawn by overriding PieChart#layoutChartChildren:

chart = new PieChart(pieChartData) {
  @Override
  protected void layoutChartChildren(double top, double left, double contentWidth, double contentHeight) {
    if (getLabelsVisible()) {
      getData().forEach(d -> {
        Optional<Node> opTextNode = chart.lookupAll(".chart-pie-label").stream().filter(n -> n instanceof Text && ((Text) n).getText().contains(d.getName())).findAny();
        if (opTextNode.isPresent()) {
          ((Text) opTextNode.get()).setText(d.getName() + " " + d.getPieValue() + " Tons");
        }
      });
    }
    super.layoutChartChildren(top, left, contentWidth, contentHeight);
  }
};

这篇关于在饼图中显示其他值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆