Menu

Do you like a chart like this ?

trung
2011-10-24
2013-06-12
1 2 3 4 > >> (Page 1 of 4)
  • trung

    trung - 2011-10-24

    Hello all,

     
  • trung

    trung - 2011-10-24

     
  • Kushal Patel

    Kushal Patel - 2011-10-24

    Umm.. what chart?

    Did you get your chart addon working (hopefully with easier instructions this time as i failed the last).

    Please do update us on your progress! ;)

     
  • trung

    trung - 2011-10-24

    I have bad experience with posting a picture, it never appears correctly.
    Anyone know the way to post picture in this forum?

     
  • yassyboy

    yassyboy - 2011-10-24

    Great job Trungt .. keep up the good work.. really impressive.

    Have you tried this with fusion charts ..  It will really look amzing with fusion animation..just a thought..

    KR..YB

     
  • Kushal Patel

    Kushal Patel - 2011-10-25

    That is awesome! Good job!

    Any instructions for us to implement ourselves?

     
  • Kushal Patel

    Kushal Patel - 2011-10-25

    Trungvt,
    You're using the FM4200 as it shows on the graph, i was wondering if you're getting all your data using the canbus connection or…. ?

     
  • trung

    trung - 2011-10-25

    Hi Kushalx,
    I get all from FM except CANBUS just because I don't have any truck with this CAN interface.
    Without CAN you can get temperature, driver ID, analog input, etc.

    I try to simplified instruction and post soonest.

     
  • trung

    trung - 2011-10-26

    1. Assum that we already have some thing to draw a chart: Speed, BatteryVolts, Temperature, analog inputs, digital inputs or something in Event Detail Report. In this example I use Speed and Altitude.

    2. Download jfreechart-1.0.13.jar and jcommon-1.0.16.jar, copy these 2 files into C:\OpenGTS_2.3.8\lib and into TomCat lib folder C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib. Restart TomCat.

    3. In file EventDetailReport.java add getSupportsGraphDisplay() method, you can put at the end of this file, just before } character. This is to enable the link to Graph

    public boolean getSupportsGraphDisplay()    //add by vtt
                {
              return true;
                }
    

    4. In file ReportDisplay.java  modify one line of method writePage.
    Line

    this._writeReportGraph(response, reqState, report, i18n);
    

    should be modify to

    EventChart._writeReportGraph(response, reqState, report, i18n);
    

    5. In C:\OpenGTS_2.3.8\src\org\opengts\war\track\page create new file  EventChart.java, codes below:

    package org.opengts.war.track.page;
    
    import java.awt.BasicStroke;
    import java.awt.Color;
    import java.io.File;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Arrays;
    import javax.servlet.http.HttpServletResponse;
    import org.opengts.db.tables.EventData;
    import org.opengts.util.HTMLTools;
    import org.opengts.util.I18N;
    import org.opengts.war.report.*;
    import org.opengts.war.tools.CommonServlet;
    import org.opengts.war.tools.RequestProperties;
    import org.jfree.chart.ChartFactory;
    import org.jfree.chart.ChartUtilities;
    import org.jfree.chart.JFreeChart;
    import org.jfree.chart.axis.NumberAxis;
    import org.jfree.chart.plot.XYPlot;
    import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
    import org.jfree.data.time.FixedMillisecond;
    import org.jfree.data.time.MovingAverage;
    import org.jfree.data.time.TimeSeries;
    import org.jfree.data.time.TimeSeriesCollection;
    import org.opengts.util.*;
    import org.opengts.dbtools.*;
    import org.opengts.db.*;
    import org.opengts.war.track.*;
    import org.opengts.db.tables.EventData;
    public class EventChart
    {
        public static void _writeReportGraph(HttpServletResponse response, final RequestProperties reqState, final ReportData report, final I18N i18ln)
        throws ReportException, IOException {
        String[] boxName = {"Speed", "Altitude", "BatVolts", "Thermo1", "Fuel%"};       //TODO from report
        String[] param   = reqState.getHttpServletRequest().getParameterValues("param");    
        if (param == null)  {           //first load 
            param = new String[1];
            param[0] = boxName[0];
        }
        Arrays.sort(param);
        String graphURL = TimeChart( report, param, boxName );  //draw a chart, get url
    
        /* write frame */
        CommonServlet.setResponseContentType(response, HTMLTools.MIME_HTML());
        PrintWriter pw = response.getWriter();    
        // HTML start
        // HTML header
        // HTML body
        pw.write("<body>\n");
        pw.write(" <FORM ACTION=\"\" METHOD=POST>\n");      //form for check boxes
        String boxChecked;
        for(int i = 0 ; i < boxName.length; i++ )  {
            boxChecked = ( Arrays.binarySearch( param, boxName[i]) >= 0 )?  "checked" : "";
            pw.write("  <INPUT TYPE=\"checkbox\" NAME=\"param\" VALUE=\""+ boxName[i]+"\" " + boxChecked+">" + boxName[i]+"\n");
        }
        
        pw.write("  <INPUT TYPE=\"submit\" VALUE=\"Generate Chart\">\n");
        pw.write(" </FORM>\n");  
        pw.write("<img src='"+graphURL+"'/>\n");
        pw.write("</body>\n");
        
        // HTML end
        pw.write("</html>\n");
        pw.close();
    }
    
        private static String TimeChart(ReportData report, String[] param, String[] boxName )
            throws ReportException, IOException{
            TimeSeries seriesSpeed    = new TimeSeries( boxName[0]);
            TimeSeries seriesAltitude = new TimeSeries( boxName[1]);            
    //      TimeSeries seriesBatVolts = new TimeSeries( boxName[2]);     //may not present in your report 
    //      TimeSeries seriesThermo1  = new TimeSeries( boxName[3]);       
    //      TimeSeries seriesFuel     = new TimeSeries( boxName[4]);
            TimeSeriesCollection dataset0 = new TimeSeriesCollection();
            TimeSeriesCollection dataset1 = new TimeSeriesCollection();
            
            for (DBDataIterator dbi = report.getBodyDataIterator(); dbi.hasNext();) {
                Object ev = dbi.next().getRowObject();
                if (ev instanceof EventData) {
                    EventData ed = (EventData)ev;
                    FixedMillisecond timestamp = new FixedMillisecond(1000*ed.getTimestamp()); //1000 
                    
                    seriesSpeed.addOrUpdate   ( timestamp , ed.getSpeedKPH());
                    seriesAltitude.addOrUpdate( timestamp , ed.getAltitude());  
    //              seriesBatVolts.addOrUpdate( timestamp , ed.getBatteryVolts());
    //              seriesThermo1.addOrUpdate ( timestamp , ed.getThermoAverage(0)); 
    //              seriesFuel.addOrUpdate  (timestamp, ed.getFuelLevel()); 
               } 
            }       //end of for 
            
            if (Arrays.binarySearch( param, boxName[0])>=0) {   //check if box0 is checked
                dataset0.addSeries(seriesSpeed);
            }                
            if (Arrays.binarySearch( param, boxName[1])>=0) {   //check if box1 is checked
                dataset1.addSeries(seriesAltitude);
            }         
    //      if (Arrays.binarySearch( param,boxName[2] )>=0) {   //may not present in your report
    //          dataset0.addSeries(seriesBatVolts);
    //       }
            
            JFreeChart chart = ChartFactory.createTimeSeriesChart(
                    report.getReportSubtitle(),             //title
                    "",             //x-axis label
                    "",             //y-axis label
                    dataset0, true, true, true
                    );
            XYPlot plot = chart.getXYPlot();
            plot.getRenderer().setSeriesStroke(0, new BasicStroke(2f));   
            plot.getRenderer().setSeriesPaint(0, Color.black);
            
            NumberAxis axis1 = new NumberAxis("");
            axis1.setTickLabelPaint(Color.red);             
            plot.setRangeAxis(1, axis1);                //add 2nd axis
            plot.setDataset(1, dataset1);
            plot.mapDatasetToRangeAxis(1, 1);
            plot.setRenderer(1, new StandardXYItemRenderer());
            plot.getRenderer(1).setSeriesStroke(0, new BasicStroke(3f));   
            String graphURL = "chart_"+ report.getFirstDeviceID() +".png";        
            File file = new File("webapps/track/"+ graphURL);
            ChartUtilities.saveChartAsPNG(file, chart, 960, 520);           //in EventDetailReport 1000,600
            
            return graphURL;
        }
    }
    

    6. Now build and deploy track.war.

    7. Run, in Event Detail Report,  above table you will see Graph link, click Graph, new chart window opened

    8. click check box to choose the chart you want

    Good luck

     
  • Peter Hamer

    Peter Hamer - 2011-10-31

    Hi,

    Many thanks for such a great addition.

    I ran the basic chart in your other post and all worked well.

    However, in this example, after following the instructions very carefully, i have the following error when clicking the chart link from the report:

    type Exception report

    message

    description The server encountered an internal error () that prevented it from fulfilling this request.

    exception

    java.io.FileNotFoundException: webapps\track\chart_t333.png (The system cannot find the path specified)
    java.io.FileOutputStream.open(Native Method)
    java.io.FileOutputStream.<init>(FileOutputStream.java:179)
    java.io.FileOutputStream.<init>(FileOutputStream.java:131)
    org.jfree.chart.ChartUtilities.saveChartAsPNG(ChartUtilities.java:318)
    org.jfree.chart.ChartUtilities.saveChartAsPNG(ChartUtilities.java:293)
    org.opengts.war.track.page.EventChart.TimeChart(EventChart.java:131)
    org.opengts.war.track.page.EventChart._writeReportGraph(EventChart.java:46)
    org.opengts.war.track.page.ReportDisplay.writePage(ReportDisplay.java:799)
    org.opengts.war.track.Track._doWork(Track.java:1458)
    org.opengts.war.track.Track._doWork_wrapper(Track.java:403)
    org.opengts.war.track.Track.doGet(Track.java:302)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.opengts.war.track.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:75)

    Any ideas? I gather it's not writing the png file.
    both jfreechart files are in the WEBINF\lib folder

    PS. i reset all the original files from the openGTS source before modifying them according to the instructions above.

    Sincerely

    Peter

     
  • trung

    trung - 2011-11-01

    Hi Peter,
    I guess that you set working path of Tomcat different with default.

    Please check in Tomcat configuration, on my computer, Working path is C:\Program Files\Apache Software Foundation\Tomcat 7.0

     
  • Anonymous

    Anonymous - 2011-11-04

    Hi,

    it is wrong to run Tomcat with security disabled and write files in the ROOT context of an application.
    With security enabled, you should create a sub-directory (in example is track/chart/) and  do:

           String prefix = "your path to $CATALINA_HOME";
           String graphURL = "/track/chart/chart_"+ report.getFirstDeviceID()+".png";       

           
           try {
                File file = new File(prefix+graphURL);
                ChartUtilities.saveChartAsPNG(file, chart, 960, 520); 
          
            }
            catch (IOException e) {
                    Print.logException("Error creating file: "+graphURL + " "+e.getLocalizedMessage(), e);
                   
            }
            return graphURL;
         
    }

    But for this to work, add in catalina.policy file:

    grant  {
        permission java.io.FilePermission "${catalina.home}/webapps/track/chart/-","read,write,delete";
    };

    Another problem would be an Array comparison with String:

    boxChecked = ( Arrays.binarySearch( param, boxName) >= 0 )? "checked" : ""; pw.write(" <INPUT TYPE=\"checkbox\" NAME=\"param\" VALUE=\""+ boxName+"\" " + boxChecked+">" + boxName+"\n"); }

    I think it should be:

    boxChecked = ( Arrays.binarySearch( param, boxName) >= 0 )? "checked" : ""; pw.write(" <INPUT TYPE=\"checkbox\" NAME=\"param\" VALUE=\""+ boxName+"\" " + boxChecked+">" + boxName_+"\n"); }

    Mihai

    _

     
  • Anonymous

    Anonymous - 2011-11-04

    hmm, you experienced the same formatting problems when posting,

    boxName\[i\]
    
     
  • wmc

    wmc - 2012-01-07

    Dear Mihai,

    In your previous post, you copied the same statement: -

    Another problem would be an Array comparison with String:

    boxChecked = ( Arrays.binarySearch( param, boxName) >= 0 )? "checked" : ""; pw.write(" <INPUT TYPE=\"checkbox\" NAME=\"param\" VALUE=\""+ boxName+"\" " + boxChecked+">" + boxName+"\n"); }

    I think it should be:
    boxChecked = ( Arrays.binarySearch( param, boxName) >= 0 )? "checked" : ""; pw.write(" <INPUT TYPE=\"checkbox\" NAME=\"param\" VALUE=\""+ boxName+"\" " + boxChecked+">" + boxName+"\n"); }

    Did you mean to write something different in the second statement?

    Am still stuck here with the errors below and will apppreciate alot if you can assist with the correct syntax for the above statement.

    See below the errors am getting: -

    java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.lang.String
    java.lang.String.compareTo(String.java:109)
    java.util.Arrays.binarySearch0(Arrays.java:2004)
    java.util.Arrays.binarySearch(Arrays.java:1946)
    org.opengts.war.track.page.EventChart._writeReportGraph(EventChart.java:59)
    org.opengts.war.track.page.ReportDisplay.writePage(ReportDisplay.java:788)
    org.opengts.war.track.Track._doWork(Track.java:1408)
    org.opengts.war.track.Track._doWork_wrapper(Track.java:358)
    org.opengts.war.track.Track.doGet(Track.java:288)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.opengts.war.track.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:75)

    Regards,
    w.mutinda

     
  • trung

    trung - 2012-01-07

    should be boxName

     
  • wmc

    wmc - 2012-01-07

    Hi trungvt,

    Thanks. Now the graph widnow gets displayed with the various checkboxes but the graph itself doesnt get displayed. When l check in the path l have configured (i.e. /usr/local/apache-tomcat-6.0.32/webapps/gtse_charts), l find that the png file_ chart_kaw918n.png_ has been generated and opens well.

    What do you think could be the issue?

    Regards

     
  • wmc

    wmc - 2012-01-07

    Find below the output if you try to open the file from browser..

    <html><head><title>Apache Tomcat/6.0.32 - Error report</title><style><!-H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}-></style> </head><body><h1>HTTP Status 404 - /usr/local/apache-tomcat-6.0.32/webapps/gtsecharts/chart_kaw918n.png</h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u>/usr/local/apache-tomcat-6.0.32/webapps/gtsecharts/chart_kaw918n.png</u></p><p><b>description</b> <u>The requested resource (/usr/local/apache-tomcat-6.0.32/webapps/gtsecharts/chart_kaw918n.png) is not available.</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/6.0.32</h3></body></html>

    Seems to be an access-related issue or what could it be?

    Regards.

     
  • trung

    trung - 2012-01-08

    Hi wemmic

    I think wrong path setting in your code, will work with simple path like this:

        pw.write("<img src='"+graphURL+"'/>\n");

     
  • wmc

    wmc - 2012-01-11

    Hi guys,

    l still havent managed to get this to work properly. Here is what am doing: -

    1. Am setting the path for the png to {my tomcat home}/webapps/gtsecharts
    2. l have then added the following in my catalina.policy file…

    //grant permission to opengts chart temp storage location
    grant  {
        permission java.io.FilePermission "${catalina.home}/webapps/gtsecharts/-","read,write,delete";
    };

    However, the PNG is generated and saved and if transferred to windows can open using any windows picture viewer. However, it doesnt diplay on the Graph page… it only displays an error icon to show the file could not be located or something like that.

    Mihai/trungvt,
    Have you successfully tested this in any linux platform with tomcat security enabled?

    Regards.

     
  • wmc

    wmc - 2012-01-11

    Hi guys,

    Sorry l forgot to paste the source of the html page generated for the graph. Just a question… is the format of the image src (see bold below) correct or it should start with something like http://xxx or file:////xxx ?

    ==========================================

    <body>
    <FORM ACTION="" METHOD=POST>
      <INPUT TYPE="checkbox" NAME="param" VALUE="Speed" checked>Speed
      <INPUT TYPE="checkbox" NAME="param" VALUE="Altitude" >Altitude
      <INPUT TYPE="checkbox" NAME="param" VALUE="BatVolts" >BatVolts
      <INPUT TYPE="checkbox" NAME="param" VALUE="Thermo1" >Thermo1
      <INPUT TYPE="checkbox" NAME="param" VALUE="Fuel%" >Fuel%
      <INPUT TYPE="submit" VALUE="Generate Chart">
    </FORM>
    <img src='/usr/local/apache-tomcat-6.0.32/webapps/gtsecharts/chart_kaw918n.png'/>
    </body>
    </html>

    ==========================================

    Regards.

     
  • trung

    trung - 2012-01-11

    How about this line
    pw.write("<img src='"+graphURL+"'/>\n");

    You keep it the same or revised to your new path?

    I assumed that you have 2 paths in webapps
    {my tomcat home}/webapps/gtse for OpenGTS
    and
    {my tomcat home}/webapps/gtsecharts  for png pictures
    ?

     
  • wmc

    wmc - 2012-01-11

    Hi trungvt,

    Thanks very much… actually l had missed out on that line. l have made the following change and it now works well.

    Replaced:

    String graphURL = "chart_"+ report.getFirstDeviceID() +".png";
    File file = new File("webapps/track/"+ graphURL); ChartUtilities.saveChartAsPNG(file, chart, 960, 520); //in EventDetailReport 1000,600

    return graphURL;
    }

    with…
    String prefix = "/usr/local/apache-tomcat-6.0.32/webapps/gtsecharts/";
    String graphURL = "chart_"+ report.getFirstDeviceID()+".png";       
             
           try {
                File file = new File(prefix + graphURL);
                ChartUtilities.saveChartAsPNG(file, chart, 960, 520); 
          
            }
            catch (IOException e) {
                    Print.logException("Error creating file: "+ prefix+graphURL + " "+e.getLocalizedMessage(), e);
                   
            }
          
            return graphURL;
    }

    And also replaced the line:

    pw.write("<img src='"+graphURL+"'/>\n");

    with:
    pw.write("<img src='../gtsecharts/"+graphURL+"'/>\n");

    and now it works well.

    Thanks very much for your support.

    Regards.

     
1 2 3 4 > >> (Page 1 of 4)

Log in to post a comment.

MongoDB Logo MongoDB