我想在运行时根据ColorPicker输入更改线条颜色。
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.animation.AnimationTimer;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.chart.AreaChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart.Series;
import javafx.scene.control.ColorPicker;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class MainApp extends Application
{
private static final int MAX_DATA_POINTS = 50;
private Series series;
private int xSeriesData = 0;
private final ConcurrentLinkedQueue<Number> dataQ = new ConcurrentLinkedQueue<>();
private ExecutorService executor;
private AddToQueue addToQueue;
private Timeline timeline2;
private NumberAxis xAxis;
private NumberAxis yAxis;
private AreaChart<Number, Number> sc;
private void init(Stage primaryStage)
{
xAxis = new NumberAxis(0, MAX_DATA_POINTS, MAX_DATA_POINTS / 10);
xAxis.setForceZeroInRange(false);
xAxis.setAutoRanging(false);
yAxis = new NumberAxis();
yAxis.setAutoRanging(true);
sc = new AreaChart<Number, Number>(xAxis, yAxis);
sc.setAnimated(false);
sc.setId("liveAreaChart");
sc.setTitle("Animated Area Chart");
//-- Chart Series
series = new AreaChart.Series<Number, Number>();
series.setName("Area Chart Series");
sc.getData().add(series);
final ColorPicker colorPicker = new ColorPicker();
colorPicker.setOnAction(new EventHandler()
{
@Override
public void handle(Event t)
{
changeColor(colorPicker.getValue().getRed(), colorPicker.getValue().getGreen(), colorPicker.getValue().getBlue(), colorPicker.getValue().getOpacity());
}
});
VBox root = new VBox(5, colorPicker, sc);
primaryStage.setScene(new Scene(root));
}
private void changeColor(double redColor, double greenColor, double blueColor, double opacity)
{
/* int redColor=0, greenColor=127, blueColor=195;
double opacity=0.4;
*/
sc.setStyle("CHART_COLOR_1: rgb(" + redColor + "," + greenColor + "," + blueColor + ");"
+ "CHART_COLOR_1_TRANS_20: rgba(" + redColor + "," + greenColor + "," + blueColor + ");");
}
@Override
public void start(Stage primaryStage) throws Exception
{
init(primaryStage);
primaryStage.show();
//-- Prepare Executor Services
//-- Prepare Executor Services
executor = Executors.newCachedThreadPool(new ThreadFactory()
{
@Override
public Thread newThread(Runnable r)
{
Thread thread = new Thread(r);
thread.setDaemon(true);
return thread;
}
});
addToQueue = new AddToQueue();
executor.execute(addToQueue);
//-- Prepare Timeline
prepareTimeline();
}
public static void main(String[] args)
{
launch(args);
}
private class AddToQueue implements Runnable
{
@Override
public void run()
{
try
{
// add a item of random data to queue
dataQ.add(Math.random());
Thread.sleep(50);
executor.execute(this);
}
catch (InterruptedException ex)
{
Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
//-- Timeline gets called in the JavaFX Main thread
private void prepareTimeline()
{
// Every frame to take any data from queue and add to chart
new AnimationTimer()
{
@Override
public void handle(long now)
{
addDataToSeries();
}
}.start();
}
private void addDataToSeries()
{
for (int i = 0; i < 20; i++)
{ //-- add 20 numbers to the plot+
if (dataQ.isEmpty())
break;
series.getData().add(new AreaChart.Data(xSeriesData++, dataQ.remove()));
}
// remove points to keep us at no more than MAX_DATA_POINTS
if (series.getData().size() > MAX_DATA_POINTS)
{
series.getData().remove(0, series.getData().size() - MAX_DATA_POINTS);
}
// update
xAxis.setLowerBound(xSeriesData - MAX_DATA_POINTS);
xAxis.setUpperBound(xSeriesData - 1);
}
}
我最不了解的部分是如何在运行时获取线条的颜色?
你能帮我实现吗?
如果我可以从上下文菜单中选择颜色,那就太棒了。
更新:选择颜色后,图表始终为黑色。
答案 0 :(得分:1)
您需要将RGB值范围从0.0 - 0.1转换为0 - 255:
private void changeColor( double redColor, double greenColor, double blueColor, double opacity )
{
int r = ( int ) Math.round( redColor * 255.0 );
int g = ( int ) Math.round( greenColor * 255.0 );
int b = ( int ) Math.round( blueColor * 255.0 );
sc.setStyle( "CHART_COLOR_1: rgb(" + r + "," + g + "," + b + ");"
+ "CHART_COLOR_1_TRANS_20: rgba(" + r + "," + g + "," + b + "," + 0.2 + ");" );
}
您还可以将ColorPicker
的初始默认颜色设置为CHART_COLOR_1
的modena.css中定义的颜色:
final ColorPicker colorPicker = new ColorPicker( Color.web( "#f3622d" ) );