Draws a block, rather than a point in jfreechart - java

i m using jfreechart to draw a graph about a logger of operations in a computer.
ex:
1:2012/09/39/28 06:55:37 8 S 0x1c0c762 Terminal --geometry=134x35 --display :0.0 --role=Terminal-0x10591b0-16869-1343137248 --show-menubar --show-borders --hide-toolbars --working-directory /home/termier "Terminal", "Terminal" "Terminal - termier#akagi: ~"
2:2012/09/39/28 06:55:41 8 S 0x1600313 /usr/lib/xfce4/notifyd/xfce4-notifyd "xfce4-notifyd", "Xfce4-notifyd" "xfce4-notifyd"
for now , i can draw every point just like (2012/09/39/28 06:55:37,Terminal),scilicet: x-axis is 2012/09/39/28 06:55:37 , Y-axis is: Terminal (i use 1 to present Terminal ,as for other commands just like Terminal... 2:/usr/lib/xfce4/notifyd/xfce4-notifyd ,etc...)
but what i need is draw a block ,like:
terminal 1: _________S||||||
/usr/lib/xfce4/notifyd/xfce4-notifyd2:______________S||||||
com 3: _____S|||||
(S:start ,eg: 2000/12/12 09:22:10 start)
.....
(when the first command end, another one will be start ,i just can get the start, it means that the post command is the end time of the previous command)
but not: 1: S
2: S
3: S
here some codes to you.
private XYDataset createDataset() {
Calendar precal;
Calendar postcal;
this.flags = modelfocus.getListflag();
commands = modelfocus.getListCommand();
DateFormat formatedate = new SimpleDateFormat("yyyy/MM/ww/dd HH:mm:ss");
precal = Calendar.getInstance();
postcal = Calendar.getInstance();
for (int i = 0; i < countCom; i++) {
this.series[i] = new TimeSeries(commands.get(i));
}
for (Focus listTxt : modelfocus.getList()) {
try {
Date d = new Date();
d = formatedate.parse(listTxt.date2String());
System.out.println(d);
precal.setTime(d);
//postcal.setTime();
} catch (ParseException e) {
System.out.println("Can't change this date");
e.printStackTrace();
}
String eachCmd = listTxt.getCommand();
for (int i = 0; i < countCom; i++) {
if (eachCmd == commands.get(i)) {
series[i].addOrUpdate(new Second(precal.getTime()),
flags.get(i));
}
}
}
TimeSeriesCollection dataset = new TimeSeriesCollection();
for (int i = 0; i < countCom; i++) {
dataset.addSeries(this.series[i]);
}
return dataset;
}
Please can someone give help to solve this problem, thank you very much.

As shown in this example, you can change the Shape used to render the values of a time series. A Rectangle is shown below.
r.setSeriesShape(0, new Rectangle(-4, -4, 9, 9));

Related

Java JFree chart realtime chart convert domain label from millis to HH:MM:SS

I created a real time chart with JFreechart where the Domain axis is epoch millis. I would like the labels to display HH:MM:SS.
Here is the block of code that I use to load the chart with data. I am very new to Java and any suggestions are very much appreciated.
Thread thread = new Thread(){
public void run() {
try (Scanner scanner = new Scanner(chosenPort.getInputStream())) { // Read Data from Serial Port
int x = 0; // Set data
while(scanner.hasNextLine()) {
long epoch = System.currentTimeMillis();
chart.getXYPlot().getDomainAxis().setRange(epoch - 30000.00, epoch + 1000.00);
try{
String line = scanner.nextLine();
int number = Integer.parseInt(line); //
series.add(epoch,number); // add Data to Chart
p1.repaint();
}catch(Exception e) {}
}
}
}
};
I was using an XYseries Line chart instead of a time series chart. By using JFreeChart chart = ChartFactory.createTimeSeriesChart instead of JFreeChart chart = ChartFactory.createXYLineChart the correct date/time values were interpreted and displayed automatically.

Find element in ArrayList Java

I have trouble finding elements, here is my code:
public static void main(String[] args) {
BufferedReader br = getFileReader("reader.csv");
ArrayList<Monitoring> col = getCollection(br);
//sort the collection on 'beginTime'
for (Monitoring x : col)
System.out.println(x.toString());
BeginTimeComparator beginTime = new BeginTimeComparator();
Collections.sort(col,beginTime);
System.out.println("Begin time:");
for (Monitoring x : col)
System.out.println(x.toString());
This is the part I have trouble with, I don't know how to search en get back the object with endTime 2015-03-10.
BTW this is one line of cvs data:
UnitId;BeginTime;EndTime;Type;Min;Max;Sum
14100072;2015-03-10 07:12:20;2015-03-10 7:13:20;Gps/GpsAccuracyGyroBias;0;0;0
//find the amount of elements that were sent on 'endTime' = 2015-03-10 (just the date)
EndTimeComparator endTime = new EndTimeComparator();
String findThis = "2015-03-10";
Collections.sort(col, endTime);
for(Monitoring x : col){
if(x.getEndTime().equals(findThis)){
System.out.println("Here is 'endTime= 2015-03-10' :");
System.out.println(x.toString());
}
}
I have tried this but both didn't work:
int index = Collections.binarySearch(col, findThis.toString(), null);
System.out.println("Here is 'endTime= 2015-03-10' :");
System.out.println(index);
Guessing that getEndTime() returns a LocalDateTime you can't compare a string with a type of LocalDateTime. You could try to parse the LocalDateTime to LocalDate and fill the 'findThis' variabel with a type of LocalDate.
Because code says more than a 1000 words:
EndTimeComparator endTime = new EndTimeComparator();
Collections.sort(col, endTime);
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate findThis = LocalDate.parse("2015-03-10", dtf);
System.out.println("Here is 'endTime= 2015-03-10' :");
for (Monitoring x : col) {
if (x.getEndTime().toLocalDate().equals(findThis)) {
System.out.println(x.toString());
}
}
You need to provide Comparator for that null or Monitoring should implement comparable (both of them should compare items by time field that you need).
Collections.binarySearch(col, findThis.toString(), null);
According to the example data you provided
UnitId;BeginTime;EndTime;Type;Min;Max;Sum
14100072;2015-03-10 07:12:20;2015-03-10 7:13:20;Gps/GpsAccuracyGyroBias;0;0;0
endTime is "2015-03-10 7:13:20", not "2015-03-10", so using equals will not work. Instead, you could try using startsWith:
String findThis = "2015-03-10";
for (Monitoring x : col) {
if (x.getEndTime().startsWith(findThis)) {
System.out.println("Here is 'endTime= 2015-03-10': ");
System.out.println(x.toString());
}
}
Or even better: Instead of storing the begin and end times as strings, convert them to Date objects or similar when you read the objects from CSV.

How to set current time in swift?

I am trying to convert pure java code into swift.But,I am having a little problem with setting the current time.I can get the current time in milliseconds.But I cant set the current time.Any help with that?
Here is my code
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
let now = NSDate()
var auctionDate = [String:String]()
let dateFormatter = NSDateFormatter()
dateFormatter.locale = NSLocale(localeIdentifier: "en_US")
var sdfDDMMYYYYEEE : NSDateFormatter!
sdfDDMMYYYYEEE.dateFormat = "dd/MM/yyyy (EEE)"
var sdfDDMMYYYY : NSDateFormatter!
sdfDDMMYYYY.dateFormat = "dd/MM/yyyy"
var sdfEEE : NSDateFormatter!
sdfEEE.dateFormat = "EEE"
// This is how i get the current time
println((now.timeIntervalSince1970)*1000 + 86400000.00)
for var i = 0; i < 5; i++ {
if sdfEEE.stringFromDate(now) == "Sun" {
// This is where i have to set my time with that ((now.timeIntervalSince1970)*1000 + 86400000.00)
i--;
continue;
}
auctionDate[(sdfDDMMYYYY.stringFromDate(now) as String)] = (sdfDDMMYYYYEEE.stringFromDate(now) as String)
// Here too I have to set my time
}
println(dateFormatter.stringFromDate(now))
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Here is the java code that I want to convert into swift code
import java.text.*;
import java.util.*;
public class DateTest{
Date now = new Date();
Map<String, String> myDate = new LinkedHashMap<String, String>();
myDate.put("", "All");
SimpleDateFormat sdfDDMMYYYYEEE = new SimpleDateFormat("dd/MM/yyyy (EEE)");
SimpleDateFormat sdfDDMMYYYY = new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat sdfEEE = new SimpleDateFormat("EEE");
for (int i = 0; i < 5; i++) {
if (sdfEEE.format(now).equals("Sun")) {
now.setTime(now.getTime() + 86400000);
i--;
continue;
}
myDate.put(sdfDDMMYYYY.format(now), sdfDDMMYYYYEEE.format(now));
now.setTime(now.getTime() + 86400000);
}
}
Any help with setting the current time in swift.I am almost closing to my answer.
In your code you want to add seconds to time eince 1970. You're in luck, there is an initializer for that: NSDate(timeIntervalSince1970: NSTimeInterval)
let now = NSDate(timeIntervalSince1970: 86400000)
This will create a new NSDate and add 86400000 seconds to the time in seconds right now since 1970.

Getting exception while using NotesCalendar.getNewInvitations

I am getting exception while invoking NotesCalendar.getNewInvitations method.
NotesException: NotesCalendar error: The database handle is NULL
at lotus.domino.local.NotesCalendar.NgetNewInvitations(Native Method)
at lotus.domino.local.NotesCalendar.getNewInvitations(Unknown Source)
at JavaAgent.main(Unknown Source)
I am trying to run this code locally to Domino Server.
import lotus.domino.*;
public class JavaAgent extends NotesThread {
public static void main(String []args){
try
{
NotesThread.sinitThread(); // start thread
Session session = NotesFactory.createSession();
System.out.println("session="+session);
// (Your code goes here)
DbDirectory dbdir = session.getDbDirectory("");
System.out.println("dbdir="+dbdir);
Database db1= session.getDatabase("server", "Conf");
NotesCalendar cal = session.getCalendar(db1);
java.util.Calendar jdt = java.util.Calendar.getInstance();
jdt.set(2015, 1, 1, 1, 1, 1);
DateTime dt1 = session.createDateTime(jdt);
DateTime dt2 = session.createDateTime("Yesterday 02");
// java.util.Vector invites = cal.getNewInvitations(dt1, dt2);
// System.out.println("invites "+invites.size());
java.util.Vector invites= cal.getEntries(dt1, dt2);
for (int j = 0; j < 3; j++) {
// Create document to post results
Document doc = db1.createDocument();
doc.appendItemValue("Form", "main");
doc.appendItemValue("subject", "New invitations");
RichTextItem body = doc.createRichTextItem("body");
if (invites.size() == 0) body.appendText("No invitations");
else {
for (int i = 0; i < invites.size(); i++) {
NotesCalendarNotice cale = (NotesCalendarNotice)invites.elementAt(i);
body.appendText(cale.read());
cale.recycle();
body.addNewLine();
}
}
doc.save(true, true);
java.util.concurrent.TimeUnit.MINUTES.sleep(30);
invites = cal.getNewInvitations(dt1, cal.getUntilTime());
} }
catch(Exception e)
{
e.printStackTrace();
}
finally
{
NotesThread.stermThread(); // must terminate every thread
}
}
}
The simple answer is getNewInvitations() doesn't make sense in the context of a resource reservation database. The method was designed to read unprocessed invitations from a mail file.
However, in the comments you say your code doesn't work for mail files either. It might be helpful to get something simpler to work and then build on that. I just tried the following code:
database = session.getDatabase(null, "mail/user.nsf");
NotesCalendar calendar = session.getCalendar(database);
// Start date is 01-Jan-2015
java.util.Calendar start = java.util.Calendar.getInstance();
start.set(2015, 0, 1);
dtStart = session.createDateTime(start);
// End date is now
java.util.Calendar end = java.util.Calendar.getInstance();
dtEnd = session.createDateTime(end);
// Get entries on the calendar between start and end
Vector<?> entries = calendar.getEntries(dtStart, dtEnd);
System.out.println("Number of entries is " + entries.size());
// Get invitations from start date (from the Inbox)
Vector<?> invites = calendar.getNewInvitations(dtStart, calendar.getUntilTime());
System.out.println("Number of invites is " + invites.size());
Everything worked as expected. Perhaps you could try the above code and see if getNewInvitations() still throws an exception.

Android Line Chart x-axis Values

I am having some problem when trying to populate data to line chart using ACHartEngine library in Android. Basically I am trying to create a line chart with x-axis from Jan-Dec no matter the amount of the particular month:
As from the x-axis, there is Jan - Dec although some of the months were with 0 amount. My problem now is I only managed to plot a graph with the months with amount. Let's say currently my database have these records:
So basically my graph will only have Jun, Aug and Sep instead of Jan - Dec.
Here is how I set up my line chart:
private void openTotalMonthlyChart() {
int[] x = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
// Creating an XYSeries for Expenses
XYSeries expensesSeries = new XYSeries("Expenses");
// Adding data to Expense Series
for (int i = 0; i < trans_list.size(); i++) {
expensesSeries.add(x[i], trans_list.get(i).getAmount());
}
// Creating a dataset to hold each series
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
// Adding Expenses Series to the dataset
dataset.addSeries(expensesSeries);
// Creating XYSeriesRenderer to customize expensesSeries
XYSeriesRenderer expensesRenderer = new XYSeriesRenderer();
expensesRenderer.setColor(Color.rgb(124, 181, 236));
expensesRenderer.setPointStyle(PointStyle.CIRCLE);
expensesRenderer.setFillPoints(true);
expensesRenderer.setLineWidth(2);
expensesRenderer.setDisplayChartValues(true);
// Creating a XYMultipleSeriesRenderer to customize the whole chart
XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
multiRenderer.setXLabels(0);
multiRenderer.setChartTitle("Total Monthly Expenses");
multiRenderer.setXTitle("");
multiRenderer.setYTitle("Amount");
multiRenderer.setZoomButtonsVisible(true);
for (int j = 0; j < trans_list.size(); j++) {
multiRenderer.addXTextLabel(j + 1, trans_list.get(j).getDate());
}
multiRenderer.addSeriesRenderer(expensesRenderer);
// Get the component from XML file
RelativeLayout chartContainer = (RelativeLayout) totalMonthlyView
.findViewById(R.id.chart_totalMonthly);
// Creating a Line Chart
chartOverall = ChartFactory.getLineChartView(getActivity(), dataset,
multiRenderer);
// Adding the Line Chart to the RelativeLayout
chartContainer.addView(chartOverall);
}
And my database SQL statement which return the data as the second image above:
public ArrayList<TransactionRecModel> getTotalMonthly() {
try {
String sql = "SELECT SUM(amount) AS total, SUBSTR(date,4,2) AS Month FROM transactionRec WHERE type = 'W' GROUP BY Month";
Cursor mCur = mDb.rawQuery(sql, null);
Log.e(TAG, "Data Grab RECORD Success");
if (mCur.getCount() != 0) {
if (mCur.moveToFirst()) {
do {
TransactionRecModel trm = new TransactionRecModel();
trm.setAmount(mCur.getInt(mCur.getColumnIndex("total")));
trm.setDate(mCur.getString(mCur.getColumnIndex("Month")));
transList.add(trm);
} while (mCur.moveToNext());
}
}
return transList;
} catch (SQLException mSQLException) {
throw mSQLException;
}
}
I wonder is there any way to do this? Thanks in advance and sorry for my poor grammar.
You can use MathHelper.NULL_VALUE for the values that you want to be skipped. See the sensor values chart AChartEngine example to see how to do this.

Categories

Resources