JavaFx create a chart from dataframe
I would like to create a chart in JavaFX. I have such a file:
And I don't know how to put "date" for x or y axis. How should I do it.
I have something like this, but I'm not sure if it is properly.
draw.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent event) {
newWindow.setTitle("Chart");
//defining the axes
if(choice1.getValue().toString()=="val" && choice2.getValue().toString()=="total"){}
final NumberAxis xAxis = new NumberAxis();
final NumberAxis yAxis = new NumberAxis();
xAxis.setLabel("Number of Month");
//creating the chart
final LineChart<Number,Number> lineChart = new LineChart<Number,Number>(xAxis,yAxis);
lineChart.setTitle("Chart");
//defining a series
XYChart.Series series = new XYChart.Series();
series.setName("My portfolio");
//populating the series with data
for(int i=0; i<d.size(); i++){
series.getData().add(new XYChart.Data(d.get(choice1.getValue().toString()).get(i), d.get(choice2.getValue().toString()).get(i)));
}
Scene scene = new Scene(lineChart,800,600);
lineChart.getData().add(series);
newWindow.setScene(scene);
newWindow.show();
}
});
LineChart linechart = new LineChart(xAxis, yAxis);
ChoiceBox choice1 = new ChoiceBox(FXCollections.observableArrayList(
"id", "date", "total", "val")
);
ChoiceBox choice2 = new ChoiceBox(FXCollections.observableArrayList(
"id", "date", "total", "val")
);
Button draw = new Button("Draw!");
HBox hb = new HBox(30);
hb.getChildren().addAll(choice1, choice2);
java file javafx charts axis
add a comment |
I would like to create a chart in JavaFX. I have such a file:
And I don't know how to put "date" for x or y axis. How should I do it.
I have something like this, but I'm not sure if it is properly.
draw.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent event) {
newWindow.setTitle("Chart");
//defining the axes
if(choice1.getValue().toString()=="val" && choice2.getValue().toString()=="total"){}
final NumberAxis xAxis = new NumberAxis();
final NumberAxis yAxis = new NumberAxis();
xAxis.setLabel("Number of Month");
//creating the chart
final LineChart<Number,Number> lineChart = new LineChart<Number,Number>(xAxis,yAxis);
lineChart.setTitle("Chart");
//defining a series
XYChart.Series series = new XYChart.Series();
series.setName("My portfolio");
//populating the series with data
for(int i=0; i<d.size(); i++){
series.getData().add(new XYChart.Data(d.get(choice1.getValue().toString()).get(i), d.get(choice2.getValue().toString()).get(i)));
}
Scene scene = new Scene(lineChart,800,600);
lineChart.getData().add(series);
newWindow.setScene(scene);
newWindow.show();
}
});
LineChart linechart = new LineChart(xAxis, yAxis);
ChoiceBox choice1 = new ChoiceBox(FXCollections.observableArrayList(
"id", "date", "total", "val")
);
ChoiceBox choice2 = new ChoiceBox(FXCollections.observableArrayList(
"id", "date", "total", "val")
);
Button draw = new Button("Draw!");
HBox hb = new HBox(30);
hb.getChildren().addAll(choice1, choice2);
java file javafx charts axis
add a comment |
I would like to create a chart in JavaFX. I have such a file:
And I don't know how to put "date" for x or y axis. How should I do it.
I have something like this, but I'm not sure if it is properly.
draw.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent event) {
newWindow.setTitle("Chart");
//defining the axes
if(choice1.getValue().toString()=="val" && choice2.getValue().toString()=="total"){}
final NumberAxis xAxis = new NumberAxis();
final NumberAxis yAxis = new NumberAxis();
xAxis.setLabel("Number of Month");
//creating the chart
final LineChart<Number,Number> lineChart = new LineChart<Number,Number>(xAxis,yAxis);
lineChart.setTitle("Chart");
//defining a series
XYChart.Series series = new XYChart.Series();
series.setName("My portfolio");
//populating the series with data
for(int i=0; i<d.size(); i++){
series.getData().add(new XYChart.Data(d.get(choice1.getValue().toString()).get(i), d.get(choice2.getValue().toString()).get(i)));
}
Scene scene = new Scene(lineChart,800,600);
lineChart.getData().add(series);
newWindow.setScene(scene);
newWindow.show();
}
});
LineChart linechart = new LineChart(xAxis, yAxis);
ChoiceBox choice1 = new ChoiceBox(FXCollections.observableArrayList(
"id", "date", "total", "val")
);
ChoiceBox choice2 = new ChoiceBox(FXCollections.observableArrayList(
"id", "date", "total", "val")
);
Button draw = new Button("Draw!");
HBox hb = new HBox(30);
hb.getChildren().addAll(choice1, choice2);
java file javafx charts axis
I would like to create a chart in JavaFX. I have such a file:
And I don't know how to put "date" for x or y axis. How should I do it.
I have something like this, but I'm not sure if it is properly.
draw.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent event) {
newWindow.setTitle("Chart");
//defining the axes
if(choice1.getValue().toString()=="val" && choice2.getValue().toString()=="total"){}
final NumberAxis xAxis = new NumberAxis();
final NumberAxis yAxis = new NumberAxis();
xAxis.setLabel("Number of Month");
//creating the chart
final LineChart<Number,Number> lineChart = new LineChart<Number,Number>(xAxis,yAxis);
lineChart.setTitle("Chart");
//defining a series
XYChart.Series series = new XYChart.Series();
series.setName("My portfolio");
//populating the series with data
for(int i=0; i<d.size(); i++){
series.getData().add(new XYChart.Data(d.get(choice1.getValue().toString()).get(i), d.get(choice2.getValue().toString()).get(i)));
}
Scene scene = new Scene(lineChart,800,600);
lineChart.getData().add(series);
newWindow.setScene(scene);
newWindow.show();
}
});
LineChart linechart = new LineChart(xAxis, yAxis);
ChoiceBox choice1 = new ChoiceBox(FXCollections.observableArrayList(
"id", "date", "total", "val")
);
ChoiceBox choice2 = new ChoiceBox(FXCollections.observableArrayList(
"id", "date", "total", "val")
);
Button draw = new Button("Draw!");
HBox hb = new HBox(30);
hb.getChildren().addAll(choice1, choice2);
java file javafx charts axis
java file javafx charts axis
edited Nov 26 '18 at 18:20
kit
1,1063816
1,1063816
asked Nov 26 '18 at 17:17
Claudia LovinClaudia Lovin
83
83
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I create an example that demo's your chart. Since you attached your data as an image, I had to create fake data. In this example, I created a DataFrame
class to hold each line of data. I added this data to a List
named fakeData
. I then create and name each Series
that will be added to the Chart
. After that, I filter the fake data by ID
. If the filtered ID
equals the Series
name, I added the data to the Series
. This chart shows Date
to Total
. If you need Date
to Val
, replace seriesList.get(i).getData().add(new XYChart.Data(dataFrame.getDate(), dataFrame.getTotal()));
with seriesList.get(i).getData().add(new XYChart.Data(dataFrame.getDate(), dataFrame.getVal()));
.
Main
import java.time.LocalDate;
import java.time.Month;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;
public class LineChartSample extends Application
{
String alphbets = "abcdefghij";
@Override
public void start(Stage stage)
{
List<DataFrame> fakeData = generateFakeDataFrames();
stage.setTitle("Line Chart Sample");
//defining the axes
final CategoryAxis xAxis = new CategoryAxis();
final NumberAxis yAxis = new NumberAxis();
xAxis.setLabel("Date");
//creating the chart
final LineChart<String, Number> lineChart = new LineChart(xAxis, yAxis);
lineChart.setTitle("Fake Chart");
//defining a series
List<XYChart.Series<String, Number>> seriesList = new ArrayList();
for (int i = 0; i < alphbets.length(); i++) {
XYChart.Series<String, Number> series = new XYChart.Series();
series.setName(alphbets.charAt(i) + "");
seriesList.add(series);
}
for (int i = 0; i < seriesList.size(); i++) {
char tempChar = alphbets.charAt(i);
//Filter the fake data
List<DataFrame> subList = fakeData.stream().filter((t) -> {
return t.getId() == tempChar;
}).collect(Collectors.toList());
//Add the filtered data to the correct series
for (DataFrame dataFrame : subList) {
System.out.println(seriesList.get(i).getName() + " :" + dataFrame.getId() + " " + dataFrame.getDate() + " " + dataFrame.getTotal());//Check to see if data is correct.
seriesList.get(i).getData().add(new XYChart.Data(dataFrame.getDate(), dataFrame.getTotal()));
}
}
Scene scene = new Scene(lineChart, 800, 600);
lineChart.getData().addAll(seriesList);
stage.setScene(scene);
stage.show();
}
public static void main(String args)
{
launch(args);
}
public List<LocalDate> createDates()
{
List<LocalDate> localDates = new ArrayList();
LocalDate localDate = LocalDate.of(1985, Month.FEBRUARY, 4);
for (int i = 0; i <= 4; i++) {
localDates.add(localDate.plusDays(i));
}
return localDates;
}
public List<DataFrame> generateFakeDataFrames()
{
Random random = new Random();
List<DataFrame> dataFrames = new ArrayList();
List<LocalDate> dates = createDates();
for (int i = 0; i < 50; i++) {
double total = (random.nextDouble() * 2 - 1) * 3;
double val = (random.nextDouble() * 2 - 1) * 2000;
dataFrames.add(new DataFrame(alphbets.charAt(i % 10), dates.get(i / 10).toString(), total, val));
}
return dataFrames;
}
}
DataFrame Class
/**
*
* @author blj0011
*/
public class DataFrame
{
private char id;
private String date;
private double total;
private double val;
public DataFrame(char id, String date, double total, double val)
{
this.id = id;
this.date = date;
this.total = total;
this.val = val;
}
public double getVal()
{
return val;
}
public void setVal(double val)
{
this.val = val;
}
public char getId()
{
return id;
}
public void setId(char id)
{
this.id = id;
}
public String getDate()
{
return date;
}
public void setDate(String date)
{
this.date = date;
}
public double getTotal()
{
return total;
}
public void setTotal(double total)
{
this.total = total;
}
@Override
public String toString()
{
return id + ", " + date + ", " + total + ", " + date + ", " + val;
}
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53486066%2fjavafx-create-a-chart-from-dataframe%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I create an example that demo's your chart. Since you attached your data as an image, I had to create fake data. In this example, I created a DataFrame
class to hold each line of data. I added this data to a List
named fakeData
. I then create and name each Series
that will be added to the Chart
. After that, I filter the fake data by ID
. If the filtered ID
equals the Series
name, I added the data to the Series
. This chart shows Date
to Total
. If you need Date
to Val
, replace seriesList.get(i).getData().add(new XYChart.Data(dataFrame.getDate(), dataFrame.getTotal()));
with seriesList.get(i).getData().add(new XYChart.Data(dataFrame.getDate(), dataFrame.getVal()));
.
Main
import java.time.LocalDate;
import java.time.Month;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;
public class LineChartSample extends Application
{
String alphbets = "abcdefghij";
@Override
public void start(Stage stage)
{
List<DataFrame> fakeData = generateFakeDataFrames();
stage.setTitle("Line Chart Sample");
//defining the axes
final CategoryAxis xAxis = new CategoryAxis();
final NumberAxis yAxis = new NumberAxis();
xAxis.setLabel("Date");
//creating the chart
final LineChart<String, Number> lineChart = new LineChart(xAxis, yAxis);
lineChart.setTitle("Fake Chart");
//defining a series
List<XYChart.Series<String, Number>> seriesList = new ArrayList();
for (int i = 0; i < alphbets.length(); i++) {
XYChart.Series<String, Number> series = new XYChart.Series();
series.setName(alphbets.charAt(i) + "");
seriesList.add(series);
}
for (int i = 0; i < seriesList.size(); i++) {
char tempChar = alphbets.charAt(i);
//Filter the fake data
List<DataFrame> subList = fakeData.stream().filter((t) -> {
return t.getId() == tempChar;
}).collect(Collectors.toList());
//Add the filtered data to the correct series
for (DataFrame dataFrame : subList) {
System.out.println(seriesList.get(i).getName() + " :" + dataFrame.getId() + " " + dataFrame.getDate() + " " + dataFrame.getTotal());//Check to see if data is correct.
seriesList.get(i).getData().add(new XYChart.Data(dataFrame.getDate(), dataFrame.getTotal()));
}
}
Scene scene = new Scene(lineChart, 800, 600);
lineChart.getData().addAll(seriesList);
stage.setScene(scene);
stage.show();
}
public static void main(String args)
{
launch(args);
}
public List<LocalDate> createDates()
{
List<LocalDate> localDates = new ArrayList();
LocalDate localDate = LocalDate.of(1985, Month.FEBRUARY, 4);
for (int i = 0; i <= 4; i++) {
localDates.add(localDate.plusDays(i));
}
return localDates;
}
public List<DataFrame> generateFakeDataFrames()
{
Random random = new Random();
List<DataFrame> dataFrames = new ArrayList();
List<LocalDate> dates = createDates();
for (int i = 0; i < 50; i++) {
double total = (random.nextDouble() * 2 - 1) * 3;
double val = (random.nextDouble() * 2 - 1) * 2000;
dataFrames.add(new DataFrame(alphbets.charAt(i % 10), dates.get(i / 10).toString(), total, val));
}
return dataFrames;
}
}
DataFrame Class
/**
*
* @author blj0011
*/
public class DataFrame
{
private char id;
private String date;
private double total;
private double val;
public DataFrame(char id, String date, double total, double val)
{
this.id = id;
this.date = date;
this.total = total;
this.val = val;
}
public double getVal()
{
return val;
}
public void setVal(double val)
{
this.val = val;
}
public char getId()
{
return id;
}
public void setId(char id)
{
this.id = id;
}
public String getDate()
{
return date;
}
public void setDate(String date)
{
this.date = date;
}
public double getTotal()
{
return total;
}
public void setTotal(double total)
{
this.total = total;
}
@Override
public String toString()
{
return id + ", " + date + ", " + total + ", " + date + ", " + val;
}
}
add a comment |
I create an example that demo's your chart. Since you attached your data as an image, I had to create fake data. In this example, I created a DataFrame
class to hold each line of data. I added this data to a List
named fakeData
. I then create and name each Series
that will be added to the Chart
. After that, I filter the fake data by ID
. If the filtered ID
equals the Series
name, I added the data to the Series
. This chart shows Date
to Total
. If you need Date
to Val
, replace seriesList.get(i).getData().add(new XYChart.Data(dataFrame.getDate(), dataFrame.getTotal()));
with seriesList.get(i).getData().add(new XYChart.Data(dataFrame.getDate(), dataFrame.getVal()));
.
Main
import java.time.LocalDate;
import java.time.Month;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;
public class LineChartSample extends Application
{
String alphbets = "abcdefghij";
@Override
public void start(Stage stage)
{
List<DataFrame> fakeData = generateFakeDataFrames();
stage.setTitle("Line Chart Sample");
//defining the axes
final CategoryAxis xAxis = new CategoryAxis();
final NumberAxis yAxis = new NumberAxis();
xAxis.setLabel("Date");
//creating the chart
final LineChart<String, Number> lineChart = new LineChart(xAxis, yAxis);
lineChart.setTitle("Fake Chart");
//defining a series
List<XYChart.Series<String, Number>> seriesList = new ArrayList();
for (int i = 0; i < alphbets.length(); i++) {
XYChart.Series<String, Number> series = new XYChart.Series();
series.setName(alphbets.charAt(i) + "");
seriesList.add(series);
}
for (int i = 0; i < seriesList.size(); i++) {
char tempChar = alphbets.charAt(i);
//Filter the fake data
List<DataFrame> subList = fakeData.stream().filter((t) -> {
return t.getId() == tempChar;
}).collect(Collectors.toList());
//Add the filtered data to the correct series
for (DataFrame dataFrame : subList) {
System.out.println(seriesList.get(i).getName() + " :" + dataFrame.getId() + " " + dataFrame.getDate() + " " + dataFrame.getTotal());//Check to see if data is correct.
seriesList.get(i).getData().add(new XYChart.Data(dataFrame.getDate(), dataFrame.getTotal()));
}
}
Scene scene = new Scene(lineChart, 800, 600);
lineChart.getData().addAll(seriesList);
stage.setScene(scene);
stage.show();
}
public static void main(String args)
{
launch(args);
}
public List<LocalDate> createDates()
{
List<LocalDate> localDates = new ArrayList();
LocalDate localDate = LocalDate.of(1985, Month.FEBRUARY, 4);
for (int i = 0; i <= 4; i++) {
localDates.add(localDate.plusDays(i));
}
return localDates;
}
public List<DataFrame> generateFakeDataFrames()
{
Random random = new Random();
List<DataFrame> dataFrames = new ArrayList();
List<LocalDate> dates = createDates();
for (int i = 0; i < 50; i++) {
double total = (random.nextDouble() * 2 - 1) * 3;
double val = (random.nextDouble() * 2 - 1) * 2000;
dataFrames.add(new DataFrame(alphbets.charAt(i % 10), dates.get(i / 10).toString(), total, val));
}
return dataFrames;
}
}
DataFrame Class
/**
*
* @author blj0011
*/
public class DataFrame
{
private char id;
private String date;
private double total;
private double val;
public DataFrame(char id, String date, double total, double val)
{
this.id = id;
this.date = date;
this.total = total;
this.val = val;
}
public double getVal()
{
return val;
}
public void setVal(double val)
{
this.val = val;
}
public char getId()
{
return id;
}
public void setId(char id)
{
this.id = id;
}
public String getDate()
{
return date;
}
public void setDate(String date)
{
this.date = date;
}
public double getTotal()
{
return total;
}
public void setTotal(double total)
{
this.total = total;
}
@Override
public String toString()
{
return id + ", " + date + ", " + total + ", " + date + ", " + val;
}
}
add a comment |
I create an example that demo's your chart. Since you attached your data as an image, I had to create fake data. In this example, I created a DataFrame
class to hold each line of data. I added this data to a List
named fakeData
. I then create and name each Series
that will be added to the Chart
. After that, I filter the fake data by ID
. If the filtered ID
equals the Series
name, I added the data to the Series
. This chart shows Date
to Total
. If you need Date
to Val
, replace seriesList.get(i).getData().add(new XYChart.Data(dataFrame.getDate(), dataFrame.getTotal()));
with seriesList.get(i).getData().add(new XYChart.Data(dataFrame.getDate(), dataFrame.getVal()));
.
Main
import java.time.LocalDate;
import java.time.Month;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;
public class LineChartSample extends Application
{
String alphbets = "abcdefghij";
@Override
public void start(Stage stage)
{
List<DataFrame> fakeData = generateFakeDataFrames();
stage.setTitle("Line Chart Sample");
//defining the axes
final CategoryAxis xAxis = new CategoryAxis();
final NumberAxis yAxis = new NumberAxis();
xAxis.setLabel("Date");
//creating the chart
final LineChart<String, Number> lineChart = new LineChart(xAxis, yAxis);
lineChart.setTitle("Fake Chart");
//defining a series
List<XYChart.Series<String, Number>> seriesList = new ArrayList();
for (int i = 0; i < alphbets.length(); i++) {
XYChart.Series<String, Number> series = new XYChart.Series();
series.setName(alphbets.charAt(i) + "");
seriesList.add(series);
}
for (int i = 0; i < seriesList.size(); i++) {
char tempChar = alphbets.charAt(i);
//Filter the fake data
List<DataFrame> subList = fakeData.stream().filter((t) -> {
return t.getId() == tempChar;
}).collect(Collectors.toList());
//Add the filtered data to the correct series
for (DataFrame dataFrame : subList) {
System.out.println(seriesList.get(i).getName() + " :" + dataFrame.getId() + " " + dataFrame.getDate() + " " + dataFrame.getTotal());//Check to see if data is correct.
seriesList.get(i).getData().add(new XYChart.Data(dataFrame.getDate(), dataFrame.getTotal()));
}
}
Scene scene = new Scene(lineChart, 800, 600);
lineChart.getData().addAll(seriesList);
stage.setScene(scene);
stage.show();
}
public static void main(String args)
{
launch(args);
}
public List<LocalDate> createDates()
{
List<LocalDate> localDates = new ArrayList();
LocalDate localDate = LocalDate.of(1985, Month.FEBRUARY, 4);
for (int i = 0; i <= 4; i++) {
localDates.add(localDate.plusDays(i));
}
return localDates;
}
public List<DataFrame> generateFakeDataFrames()
{
Random random = new Random();
List<DataFrame> dataFrames = new ArrayList();
List<LocalDate> dates = createDates();
for (int i = 0; i < 50; i++) {
double total = (random.nextDouble() * 2 - 1) * 3;
double val = (random.nextDouble() * 2 - 1) * 2000;
dataFrames.add(new DataFrame(alphbets.charAt(i % 10), dates.get(i / 10).toString(), total, val));
}
return dataFrames;
}
}
DataFrame Class
/**
*
* @author blj0011
*/
public class DataFrame
{
private char id;
private String date;
private double total;
private double val;
public DataFrame(char id, String date, double total, double val)
{
this.id = id;
this.date = date;
this.total = total;
this.val = val;
}
public double getVal()
{
return val;
}
public void setVal(double val)
{
this.val = val;
}
public char getId()
{
return id;
}
public void setId(char id)
{
this.id = id;
}
public String getDate()
{
return date;
}
public void setDate(String date)
{
this.date = date;
}
public double getTotal()
{
return total;
}
public void setTotal(double total)
{
this.total = total;
}
@Override
public String toString()
{
return id + ", " + date + ", " + total + ", " + date + ", " + val;
}
}
I create an example that demo's your chart. Since you attached your data as an image, I had to create fake data. In this example, I created a DataFrame
class to hold each line of data. I added this data to a List
named fakeData
. I then create and name each Series
that will be added to the Chart
. After that, I filter the fake data by ID
. If the filtered ID
equals the Series
name, I added the data to the Series
. This chart shows Date
to Total
. If you need Date
to Val
, replace seriesList.get(i).getData().add(new XYChart.Data(dataFrame.getDate(), dataFrame.getTotal()));
with seriesList.get(i).getData().add(new XYChart.Data(dataFrame.getDate(), dataFrame.getVal()));
.
Main
import java.time.LocalDate;
import java.time.Month;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;
public class LineChartSample extends Application
{
String alphbets = "abcdefghij";
@Override
public void start(Stage stage)
{
List<DataFrame> fakeData = generateFakeDataFrames();
stage.setTitle("Line Chart Sample");
//defining the axes
final CategoryAxis xAxis = new CategoryAxis();
final NumberAxis yAxis = new NumberAxis();
xAxis.setLabel("Date");
//creating the chart
final LineChart<String, Number> lineChart = new LineChart(xAxis, yAxis);
lineChart.setTitle("Fake Chart");
//defining a series
List<XYChart.Series<String, Number>> seriesList = new ArrayList();
for (int i = 0; i < alphbets.length(); i++) {
XYChart.Series<String, Number> series = new XYChart.Series();
series.setName(alphbets.charAt(i) + "");
seriesList.add(series);
}
for (int i = 0; i < seriesList.size(); i++) {
char tempChar = alphbets.charAt(i);
//Filter the fake data
List<DataFrame> subList = fakeData.stream().filter((t) -> {
return t.getId() == tempChar;
}).collect(Collectors.toList());
//Add the filtered data to the correct series
for (DataFrame dataFrame : subList) {
System.out.println(seriesList.get(i).getName() + " :" + dataFrame.getId() + " " + dataFrame.getDate() + " " + dataFrame.getTotal());//Check to see if data is correct.
seriesList.get(i).getData().add(new XYChart.Data(dataFrame.getDate(), dataFrame.getTotal()));
}
}
Scene scene = new Scene(lineChart, 800, 600);
lineChart.getData().addAll(seriesList);
stage.setScene(scene);
stage.show();
}
public static void main(String args)
{
launch(args);
}
public List<LocalDate> createDates()
{
List<LocalDate> localDates = new ArrayList();
LocalDate localDate = LocalDate.of(1985, Month.FEBRUARY, 4);
for (int i = 0; i <= 4; i++) {
localDates.add(localDate.plusDays(i));
}
return localDates;
}
public List<DataFrame> generateFakeDataFrames()
{
Random random = new Random();
List<DataFrame> dataFrames = new ArrayList();
List<LocalDate> dates = createDates();
for (int i = 0; i < 50; i++) {
double total = (random.nextDouble() * 2 - 1) * 3;
double val = (random.nextDouble() * 2 - 1) * 2000;
dataFrames.add(new DataFrame(alphbets.charAt(i % 10), dates.get(i / 10).toString(), total, val));
}
return dataFrames;
}
}
DataFrame Class
/**
*
* @author blj0011
*/
public class DataFrame
{
private char id;
private String date;
private double total;
private double val;
public DataFrame(char id, String date, double total, double val)
{
this.id = id;
this.date = date;
this.total = total;
this.val = val;
}
public double getVal()
{
return val;
}
public void setVal(double val)
{
this.val = val;
}
public char getId()
{
return id;
}
public void setId(char id)
{
this.id = id;
}
public String getDate()
{
return date;
}
public void setDate(String date)
{
this.date = date;
}
public double getTotal()
{
return total;
}
public void setTotal(double total)
{
this.total = total;
}
@Override
public String toString()
{
return id + ", " + date + ", " + total + ", " + date + ", " + val;
}
}
answered Nov 26 '18 at 21:17
SedrickSedrick
6,19732039
6,19732039
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53486066%2fjavafx-create-a-chart-from-dataframe%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown