Export androidplot charts/graphs into excel formats - androidplot

Am using androidplot library to display dynamic/static charts & graphs within my Android app. But now I have to export those charts/graphs into excel formats. AndroidPlot library is not providing any API to export the charts/graphs. Is there anyway to do the same?
Can anybody please help me or let me know some workaround to deal with this issue.

That is not a built-in feature of Androidplot however it should be trivial to write a method to suit your needs. All you would need to do is iterate over the XYSeries and write each x/y value to a file separated by a comma. This gives you a generic CSV formatted file that Excel can open. Whether the values or interleaved in or series, how many series are contained in a single file etc, is up to you. To get you started, here's a simple routine that will convert a single XYSeries to an interleaved CSV string. All that's left to do is write the String to file:
/**
* Converts an XYSeries into CSV "x,y,x,y..." format.
* #param series The series to be converted.
* #return CSV formatted version of series.
*/
String toCsv(XYSeries series) {
StringBuffer output = new StringBuffer();
for(int i = 0; i < series.size(); i++) {
output.append(series.getX(i).toString());
output.append(',');
output.append(series.getY(i)).toString();
}
return output.toString();
}

Related

How to stop Python Pandas from converting specific column from int to float

trying to out put a dataframe into txt file (for a feed). a few specific columns are getting automatically converted into float instead of int as intented.
how can i specific those columns to use int as dtype?
i tried to output the whole dataframd as string and that did not work.
the columns i would like to specify are named [CID1] and [CID2]
data = pd.read_sql(sql,conn)
data = data.astype(str)
data.to_csv('data_feed_feed.txt', sep ='\t',index=True)
Based on the code you provided, you turn all of your data to strings just before export.
Thus, you either need to turn some cols back to desired type, such as:
data["CID1"] = data["CID1"].astype(int)
or not convert them in the first place.
It is not clear from what you provided why you'd have issues with ints being converted to floats.
this post provides heaps of info:
stackoverflow.com/a/28648923/9249533

Convert incoming byte stream into integers and export to Excel

I am very new to programming and am working on an Arduino project to measure wavelengths of light.
I am using the Spectruino 3 and am trying to read in the bytes that it records into an array, convert the array into an integer array and then export that data to excel. Currently I have this:
const int numBytes = 501;
int bytestream[numBytes];
void setup(){
Serial.begin(115200);
}
void loop() {
for(int i=0; i<numBytes; i++){
bytestream[i]=Serial.read();
}
}
My spectrometer currently doesn't work, but this seems to compile. I was just wondering how to convert this bytestream array into an integer array and then export it into Excel.
Excel and other, better spreadsheet programs can load text file based CSV files.
That means you have 2 options:
Convert data to CSV rows in device.
Send text data over serial line and use any serial port program on PC that can receive to file to catch the data.
Open CSV file in Excel or other, better spreadsheet program.
Or
Send raw binary data over serial line and use any serial port program on PC that can receive to file to catch the data.
Convert data to CSV file with your favorite scripting language (like Python).
Open CSV file in Excel or other, better spreadsheet program.

Treat all cells as strings while using the Apache POI XSSF API

I'm using the Apache POI framework for parsing large Excel spreadsheets. I'm using this example code as a guide: XLSX2CSV.java
I'm finding that cells that contain just numbers are implicitly being treated as numeric fields, while I wanted them to be treated always as strings. So rather than getting 1.00E+13 (which I'm currently getting) I'll get the original string value: 10020300000000.
The example code uses a XSSFSheetXMLHandler which is passed an instance of DataFormatter. Is there a way to use that DataFormatter to treat all cells as strings?
Or as an alternative: in the implementation of the interface SheetContentsHandler.cell method there is string value that is the cellReference. Is there a way to convert a cellReference into an index so that I can use the SharedStringsTable.getEntryAt(int idx) method to read directly from the strings table?
To reproduce the issue, just run the sample code on an xlsx file of your choice with a number like the one in my example above.
UPDATE: It turns out that the string value I get seems to match what you would see in Excel. So I guess that's going to be "good enough" generally. I'd expect the data I'm sent to "look right" and therefore it'll get parsed correctly. However, I'm sure there will be mistakes and in those cases it'd be nice if I could get at the raw string value using the streaming API.
To resolve this issue I created my own class based on XSSFSheetXMLHandler
I copied that class, renamed it and then in the endElement method I changed this part of the code which is formatting the raw string:
case NUMBER:
String n = value.toString();
if (this.formatString != null && n.length() > 0)
thisStr = formatter.formatRawCellContents(Double.parseDouble(n), this.formatIndex, this.formatString);
else
thisStr = n;
break;
I changed it so that it would not format the raw string:
case NUMBER:
thisStr = value.toString();
break;
Now every number in my spreadsheet has its raw value returned rather than a formatted version.

Static chart with timestamp on x-axis

I want to create a static chart of values pulled out of a MySQL database.
The chart format would be (x axis : dd/mm/yy hh:mm:ss (corresponding to timestamp of mysql database)) and y-axis would be a double value. I am able to successfully retrieve these values from MySql database.I want help plotting them by ZingChart
Nikita.
Once you've retrieved your values from your MySQL database, you'll want to convert the MySQL date values in to Unix time in milliseconds. I've populated a $date array with the MySQL date values, and iterated over the array, calling strtotime to first convert to Unix time, and multiplying by 1000 to convert to milliseconds. In order to be able to directly modify array elements within the loop, I've also preceded $value with to assign by reference.
foreach ($date as &$value){
$value = strtotime( $value ) * 1000;
}
So now that the values in the $date array have been converted to the proper format, it's time to create a JavaScript array from the PHP array. This can be done using join():
var dateValues = [<?php echo join($date, ',') ?>];
The resulting array looks like this:
var dateValues = [1356994800000,1357081200000,1357167600000, ... ];
To use this array in ZingChart, use the dateValues variable with "values" in the scale-x object. To convert the Unix time values back to dates in ZingChart, add the "transform" object, and set it to "type":"date".
"scale-x":{
"values": dateValues,
"transform":{
"type":"date",
"item":{
"visible":false
}
}
},
...
That takes care of the scale. To get your other values in the chart, you do pretty much the same thing. Convert the PHP arrays into JavaScript arrays, and use the array variable in your chart JSON.
With the PHP $series array:
var seriesValues = [<?php echo join($series, ',') ?>];
In your chart JSON:
"series":[
{
"values":seriesValues
}
]
I've compiled all of this in to a Github Gist for you. Let me know if you have any questions!
Check out our demos repo on GitHub. We have a tutorial specifically about connecting to a MySQL database with PHP.
There's a step-by-step walkthrough on our site, too.
If you share your JSON or more details about it, I can help you with putting your chart together.
I'm on the ZingChart team. Please let me know if you have other questions.

Excel wrongly converts ranges into dates, how to avoid it?

I have a .tsv file with some fields being ranges like 1 - 4. I want to read these fields as they are textually written. However, upon file opening excel converts automatically those range fields to dates. For instance 1 - 4 is converted to 4-Jan. If I try to format back the cell to another type, the value is already changed and I can only get a useless number (39816). Even if the range fields are within double quotes, the wrong conversion to date still takes place. How to avoid this behavior?
I think you best use the import facility in excel but you may have to manually change the file extension to a csv.
When importing be sure to select text for all the columns with these values.
My question is in fact a duplicate of at least:
1) Stop Excel from automatically converting certain text values to dates
2) Excel: Default to TEXT rather than GENERAL when opening a .csv file
The possible solutions for Excel are to 1) either writing the fields with special double quotes like "May 16, 2011" as "=""May 16, 2011""" or 2) importing the csv/tsv file with the external data wizard and then selecting manually which columns you want to read as TEXT and not GENERAL (which could convert fields to dates)
As for my use case, I was only using Excel to remove some columns. None of the solutions was appealing to me because I wouldn't like to rewrite the tsv files with special quotes and because I had hundreds of columns and I didn't want to select each manually to be read as TEXT.
Therefore I wrote a scala script to filter tsv files by column names:
package com.jmcejuela.ml
import java.io.InputStream
import java.io.Writer
import scala.io.Codec
import scala.io.Source
import Table._
/**
* Class to represent tables with a fixed size of columns. All rows have the same columns.
*/
class Table(val rows: Seq[Row]) {
lazy val numDiffColumns = rows.foldLeft(Set[Int]())((set, row) => set + row.size)
def toTSV(out: Writer) {
if (rows.isEmpty) out.write(TableEmpty.toString)
else {
out.write(writeLineTSV(rows.head.map(_.name))) //header
rows.foreach(r => out.write(writeLineTSV(r.map(_.value))))
out.close
}
}
/**
* Get a Table with only the given columns.
*/
def filterColumnsByName(columnNames: Set[String]): Table = {
val existingNames = rows.head.map(_.name).toSet
assert(columnNames.forall(n => existingNames.contains(n)), "You want to include column names that do not exist")
new Table(rows.map { row => row.filter(col => columnNames.contains(col.name)) })
}
}
object TableEmpty extends Table(Seq.empty) {
override def toString = "Table(Empty)"
}
object Table {
def apply(rows: Row*) = new Table(rows)
type Row = Array[Column]
/**
* Column representation. Note that each column has a name and a value. Since the class Table
* is a sequence of rows which are a size-fixed array of columns, the name field is redundant
* for Table. However, this column representation could be used in the future to support
* schemata-less tables.
*/
case class Column(name: String, value: String)
private def parseLineTSV(line: String) = line.split("\t")
private def writeLineTSV(line: Seq[String]) = line.mkString("", "\t", "\n")
/**
* It is assumed that the first row gives the names to the columns
*/
def fromTSV(in: InputStream)(implicit encoding: Codec = Codec.UTF8): Table = {
val linesIt = Source.fromInputStream(in).getLines
if (linesIt.isEmpty) TableEmpty
else {
val columnNames = parseLineTSV(linesIt.next)
val padding = {
//add padding of empty columns-fields to lines that do not include last fields because they are empty
def infinite[A](x: A): Stream[A] = x #:: infinite(x)
infinite("")
}
val rows = linesIt.map { line =>
((0 until columnNames.size).zip(parseLineTSV(line) ++: padding).map { case (index, field) => Column(columnNames(index), field) }).toArray
}.toStream
new Table(rows)
}
}
}
Write 01-04 instead of 1-4 in excel..
I had a "text" formatted cell in excel being populated with a chemical casn with the value "8013-07-8" that was being reformatted into a date format. To remedy the problem, I concatenated a single quote to the beginning of the value and it rendered correctly when viewing the results. When you click on the cell, you see the prefixed single-quote, but at least I stopped seeing it as a date.
In my case, When I typed 5-14 in my D2 excel cell, is coverts to date 14 May. With a help from somebody , I was able to change the date format to the number range (5-14) using the following approach and wanted to share it with you. (I will use my case an example).
Using cell format in excel, I converted the date format in D2 (14 May) to number first ( in my case it gave me 43599).
then used the formula below ,in excel, to convert it 5-14.
=IF (EXACT (D2, 43599), "5-14", D2).

Resources