Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
X
XChart
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Brady James Garvin
XChart
Commits
e514f0e0
Commit
e514f0e0
authored
11 years ago
by
Ross Jourdain
Browse files
Options
Downloads
Patches
Plain Diff
First attempt at a realtime graph
parent
959d20d4
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
xchart-demo/src/main/java/com/xeiam/xchart/standalone/RealtimeAttempt.java
+135
-0
135 additions, 0 deletions
...ain/java/com/xeiam/xchart/standalone/RealtimeAttempt.java
with
135 additions
and
0 deletions
xchart-demo/src/main/java/com/xeiam/xchart/standalone/RealtimeAttempt.java
0 → 100644
+
135
−
0
View file @
e514f0e0
/*
* Copyright 2013 Xeiam, LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
com.xeiam.xchart.standalone
;
import
com.xeiam.xchart.Chart
;
import
com.xeiam.xchart.Series
;
import
com.xeiam.xchart.SeriesMarker
;
import
com.xeiam.xchart.XChartPanel
;
import
java.util.ArrayList
;
import
java.util.Collection
;
import
java.util.Timer
;
import
java.util.TimerTask
;
import
javax.swing.JFrame
;
import
javax.swing.JPanel
;
/**
*
* @author rossjourdain
*/
public
class
RealtimeAttempt
{
private
Chart
chart
;
private
JPanel
chartPanel
;
public
static
void
main
(
String
[]
args
)
throws
Exception
{
// Setup the panel
final
RealtimeAttempt
realtimeAttempt
=
new
RealtimeAttempt
();
realtimeAttempt
.
buildPanel
();
// Schedule a job for the event-dispatching thread:
// creating and showing this application's GUI.
javax
.
swing
.
SwingUtilities
.
invokeLater
(
new
Runnable
()
{
@Override
public
void
run
()
{
// Create and set up the window.
JFrame
frame
=
new
JFrame
(
"XChart"
);
frame
.
setDefaultCloseOperation
(
JFrame
.
EXIT_ON_CLOSE
);
frame
.
add
(
realtimeAttempt
.
getChartPanel
());
// Display the window.
frame
.
pack
();
frame
.
setVisible
(
true
);
}
});
// Simulate a data feed
TimerTask
chartUpdaterTask
=
new
TimerTask
()
{
@Override
public
void
run
()
{
realtimeAttempt
.
updateData
();
}
};
Timer
timer
=
new
Timer
();
timer
.
scheduleAtFixedRate
(
chartUpdaterTask
,
0
,
1000
);
}
public
void
buildPanel
()
{
Collection
<
Number
>
yData
=
getRandomData
(
5
);
// Create Chart
chart
=
new
Chart
(
500
,
400
);
chart
.
setChartTitle
(
"Sample Chart"
);
chart
.
setXAxisTitle
(
"X"
);
chart
.
setYAxisTitle
(
"Y"
);
Series
series
=
chart
.
addSeries
(
"y(x)"
,
null
,
yData
);
series
.
setMarker
(
SeriesMarker
.
CIRCLE
);
chartPanel
=
new
XChartPanel
(
chart
);
}
public
void
updateData
()
{
// Get some new data
Collection
<
Number
>
newData
=
getRandomData
(
1
);
// Replace the existing
ArrayList
<
Number
>
replacementData
=
new
ArrayList
<
Number
>();
Series
oldSeries
=
(
Series
)
chart
.
getSeriesMap
().
values
().
toArray
()[
0
];
Collection
<
Number
>
oldData
=
oldSeries
.
getyData
();
replacementData
.
addAll
(
oldData
);
replacementData
.
addAll
(
newData
);
// Limit the total number of points
while
(
replacementData
.
size
()
>
20
)
{
replacementData
.
remove
(
0
);
}
// Swap out the data
chart
.
getSeriesMap
().
clear
();
Series
newSeries
=
chart
.
addSeries
(
oldSeries
.
getName
(),
null
,
replacementData
);
newSeries
.
setLineColor
(
oldSeries
.
getStrokeColor
());
newSeries
.
setLineStyle
(
oldSeries
.
getStroke
());
newSeries
.
setMarkerColor
(
oldSeries
.
getMarkerColor
());
//etc
// Re-display the chart
chartPanel
.
revalidate
();
chartPanel
.
repaint
();
}
public
Chart
getChart
()
{
return
chart
;
}
public
JPanel
getChartPanel
()
{
return
chartPanel
;
}
private
static
Collection
<
Number
>
getRandomData
(
int
numPoints
)
{
ArrayList
<
Number
>
data
=
new
ArrayList
<
Number
>();
for
(
int
i
=
0
;
i
<
numPoints
;
i
++)
{
data
.
add
(
Math
.
random
()
*
100
);
}
return
data
;
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment