how to display two line charts on a graphview in android getting the values from database? - android-graphview

how to display two line charts on a graphview in android getting the values from database?
here is my code
gvp1=new GraphViewData[systolic.size()];
for(int i=0;i<gvp1.length;i++)
{
gvp1[i]=new GraphViewData(Double.parseDouble(systolic.get(i).toString()), Double.parseDouble(diastolic.get(i).toString()));
System.out.println("array avp1 is......."+Double.parseDouble(systolic.get(i).toString()));
}
LineGraphView graphView = new LineGraphView(
this
, "BP Graph"
);
graphView.addSeries(new GraphViewSeries(gvp1));
System.out.println("in graphview......"+gvp1);
/*
graphView.addSeries(new GraphViewSeries(new GraphViewData[] {
new GraphViewData(1, 2.0d)
}));*/
/*
ViewHolder v = null;
v = new ViewHolder();
convertView=null;
*/
setContentView(graphView);
// setContentView(R.layout.demo11);
ImageView iv = new ImageView(this);
iv.setImageResource(R.drawable.graph_header);
graphView.addView(iv);
}
this is how i am getting values from database to graph. but i want another line graph to be plotted on same scale.
any working code will be a great help..

Just call addSeries putting the other data of line graph.
See my implementation:
GraphViewSeries line1Series = new GraphViewSeries("LINE ONE", new GraphViewSeriesStyle(Color.GREEN, 3), new GraphViewData[] { new GraphViewData(0, 0) }); // put an complete array
GraphViewSeries line2Series = new GraphViewSeries("LINE TWO", new GraphViewSeriesStyle(Color.BLUE, 3), new GraphViewData[] { new GraphViewData(0, 0) }); // put an complete array
graphView = new LineGraphView(this, "Graph with 2 lines");
graphView.addSeries(line1Series);
graphView.addSeries(line2Series);
graphView.setShowLegend(true);
graphView.setLegendAlign(LegendAlign.TOP);
graphView.setGraphViewStyle(new GraphViewStyle(Color.DKGRAY, Color.DKGRAY, Color.LTGRAY));
graphView.redrawAll();
I hope this can help you.

Related

Watermark specific page in PDF iTextSharp

I create the PDF dynamically based on the custom object data and water mark all pages in the PDF using iTextSharp 5.5.10.0 as shown in the code below.
Question: How can I watermark one or more page based on the criteria in the for loop? Custom object data has a boolean flag which indicates whether the pdf page could be watermarked?
Any help is greatly appreciated.
using (MemoryStream workStream = new MemoryStream())
{
using (Document document = new Document())
{
using (PdfWriter writer = PdfWriter.GetInstance(document, workStream))
{
HeaderFooter headerfooter = new HeaderFooter();
headerfooter.watermarkpage = WaterMarkPage;
headerfooter.includefooter = IncludeFooter;
writer.PageEvent = headerfooter;
writer.CloseStream = false;
document.Open();
PdfContentByte cb = writer.DirectContent;
foreach (TestResult testresultdata in testresults)
{
document.Add(PopulateData(testresultdata, reporttitle, includesponsordata));
// Move to next page
document.NewPage();
} // end foreach Order
document.Close();
} // end writer
} // end document
}

zxing Datamatrix generator creating rectangular barcode which can't be scanned

I am using barcodewriter to write datamatrix barcoe. While most of the times it creates correct square style datamatrix barcode, for some of text it creates rectangular shaped barcode.
For inputData like below it creates rectangular barcode
8004600000070000017
C/TH PAUL PENGELLY
C/TH NICKY PARSONS
C/TH ROSEMARIE BARTOLOME
while for others it creates square styled: CTH HEKT-WOODROW MORGAN
800460000007
800460000007000001700000
i am usinf this code to generate code:
BarcodeWriter writer = new BarcodeWriter() { Format = BarcodeFormat.DATA_MATRIX };
var img = writer.Write(inputData);
return new Bitmap(img);
img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
dto.BarcodeImage = ms.ToArray();
How can I make sure that I always get Square shaped datamatrix?
I have alread tried adding height,width options.
Thanks
There is SymbolShape option which can be used to force shape .
DatamatrixEncodingOptions options = new DatamatrixEncodingOptions()
{
Height = 300,
Width = 300,
PureBarcode = true,
Margin = 1,
SymbolShape = SymbolShapeHint.FORCE_SQUARE
};
It is not easy to detect but after careful reviewing, I found how to do it.
readonly DataMatrixWriter DMencoder = new();
readonly Dictionary<EncodeHintType, object> DMencodeType = new()
{
[EncodeHintType.DATA_MATRIX_DEFAULT_ENCODATION] = Encodation.C40, //Optional
[EncodeHintType.DATA_MATRIX_SHAPE] = SymbolShapeHint.FORCE_SQUARE
};
DMencoder.encode(matrixText, BarcodeFormat.DATA_MATRIX, 100, 100, DMencodeType)

iTextSharp does not render header/footer when using element generated by XmlWorkerHelper

I am trying to add header/footer to a PDF whose content is otherwise generated by XMLWorkerHelper. Not sure if it's a placement issue but I can't see the header/footer.
This is in an ASP.NET MVC app using iTextSharp and XmlWorker packages ver 5.4.4 from Nuget.
Code to generate PDF is as follows:
private byte[] ParseXmlToPdf(string html)
{
XhtmlToListHelper xhtmlHelper = new XhtmlToListHelper();
Document document = new Document(PageSize.A4, 30, 30, 90, 90);
MemoryStream msOutput = new MemoryStream();
PdfWriter writer = PdfWriter.GetInstance(document, msOutput);
writer.PageEvent = new TextSharpPageEventHelper();
document.Open();
var htmlContext = new HtmlPipelineContext(null);
htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
var cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(false);
cssResolver.AddCssFile(HttpContext.Server.MapPath("~/Content/themes/mytheme.css"), true);
var pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(document, writer)));
var worker = new XMLWorker(pipeline, true);
var parser = new XMLParser();
parser.AddListener(worker);
using (TextReader sr = new StringReader(html))
{
parser.Parse(sr);
}
//string text = "Some Random Text";
//for (int k = 0; k < 8; ++k)
//{
// text += " " + text;
// Paragraph p = new Paragraph(text);
// p.SpacingBefore = 8f;
// document.Add(p);
//}
worker.Close();
document.Close();
return msOutput.ToArray();
}
Now instead of using these three lines
using (TextReader sr = new StringReader(html))
{
parser.Parse(sr);
}
if I comment them out and uncomment the code to add a random Paragraph of text (commented in above sample), I see the header/footer along with the random text.
What am I doing wrong?
The EventHandler is as follows:
public class TextSharpPageEventHelper : PdfPageEventHelper
{
public Image ImageHeader { get; set; }
public override void OnEndPage(PdfWriter writer, Document document)
{
float cellHeight = document.TopMargin;
Rectangle page = document.PageSize;
PdfPTable head = new PdfPTable(2);
head.TotalWidth = page.Width;
PdfPCell c = new PdfPCell(ImageHeader, true);
c.HorizontalAlignment = Element.ALIGN_RIGHT;
c.FixedHeight = cellHeight;
c.Border = PdfPCell.NO_BORDER;
head.AddCell(c);
c = new PdfPCell(new Phrase(
DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss") + " GMT",
new Font(Font.FontFamily.COURIER, 8)
));
c.Border = PdfPCell.TOP_BORDER | PdfPCell.RIGHT_BORDER | PdfPCell.BOTTOM_BORDER | PdfPCell.LEFT_BORDER;
c.VerticalAlignment = Element.ALIGN_BOTTOM;
c.FixedHeight = cellHeight;
head.AddCell(c);
head.WriteSelectedRows(
0, -1, // first/last row; -1 flags all write all rows
0, // left offset
// ** bottom** yPos of the table
page.Height - cellHeight + head.TotalHeight,
writer.DirectContent
);
}
}
Feeling angry and stupid for having lost more than two hours of my life to Windows 8.1! Apparently the built in PDF Reader app isn't competent enough to show 'Headers'/'Footers'. Installed Foxit Reader and opened the output and it shows the Headers allright!
I guess I should try with Adobe Reader too!!!
UPDATES:
I tried opening the generated PDF in Adobe Acrobat reader and finally saw some errors. This made me look at the HTML that I was sending to the XMLWorkerHelper more closely. The structure of the HTML had <div>s inside <table>s and that was tripping up the generated PDF. I cleaned up the HTML to ensure <table> inside <table> and thereafter the PDF came out clean in all readers.
Long story short, the code above is fine, but if you are testing for PDF's correctness, have Adobe Acrobat Reader handy at a minimum. Other readers behave unpredictably as in either not reporting errors or incorrectly rendering the PDF.

android-graphview shows wrong graphs with setNumVerticalLabels

i test the android-graphview library and i find this behavior:
I use the latest GraphViewDemos and the first SimpleGraph example. It shows a linegraph with the correct data. (The y-axis values are 1,2,3)
GraphViewSeries exampleSeries = new GraphViewSeries(new GraphViewData[] {
new GraphViewData(1, 2.0d)
, new GraphViewData(2, 1.5d)
, new GraphViewData(2.5, 3.0d) // another frequency
, new GraphViewData(3, 2.5d)
, new GraphViewData(4, 1.0d)
, new GraphViewData(5, 3.0d)
});
The max value is three (Sorry i can't post an image) and all other coordinates are correct.
If i add these lines
graphView.getGraphViewStyle().setNumVerticalLabels(5);
graphView.setVerticalLabels( new String[]{"4","3","2","1","0"});
before
LinearLayout layout = (LinearLayout) findViewById(R.id.graph1);
layout.addView(graphView);
in the code to change the y-axis, i get a graph where the max-value is not still three, it's four. And all the other coordinates are wrong in the y-values.
Why does the complete graph change and not only the y-axis?
with the line:
graphView.setVerticalLabels( new String[]{"4","3","2","1","0"});
you set static labels to the graph. So the vertical labels (y-values) have no link to the data anymore.
This line is for dynamic labels. You can modify the count of the labels that will be generated.
graphView.getGraphViewStyle().setNumVerticalLabels(5);
But you are using static labels, so the line doesn't make sense.
http://android-graphview.org/
Visit this page and scroll to the Custom Label Formatter part of the tutorial.
GraphView graphView = new LineGraphView(this, "example");
graphView.setCustomLabelFormatter(new CustomLabelFormatter() {
#Override
public String formatLabel(double value, boolean isValueX) {
if (isValueX) {
if (value < 5) {
return "small";
} else if (value < 15) {
return "middle";
} else {
return "big";
}
}
return null; // let graphview generate Y-axis label for us
}
});
Basically you will have to map the actual y value with the static Vertical Label you have provided

How to load dynamic data on a eclipse shell?

I want to load details of a user's when clicking their names . So I create a Grid Layout with two columns left hand side is standalone and right hand side data will be loaded based on clicking their names .
JAVA CODE
Display display = PlatformUI.createDisplay();
Shell shell = new Shell(display);
shell.setSize(400, 400);
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 2;
shell.setLayout(gridLayout);
shell.setBackground(new Color(display, 255, 255, 255));
final Table table = new Table(shell, SWT.VIRTUAL);
table.setLayout(new GridLayout());
table.setSize(400, 400);
final TableColumn column = new TableColumn(table, SWT.LEFT);
column.setWidth(150);
column.setText("Runnable Services");
table.setItemCount(5);
final String[] services = new String[] { "Franklin", "Christian",
"Richard Levi",
"Dale Steyn", "Chris Gayle" };
table.addListener(SWT.SetData, new Listener() {
public void handleEvent(Event event) {
TableItem item = (TableItem) event.item;
int index = table.indexOf(item);
item.setText(services[index]);
System.out.println(item.getText());
}
});
table.addSelectionListener(new OnClickListener());
alignCenter(shell, display);
shell.open();
I added a selection listener on a table . When click the players I add their details on SelectionListener's widget Selected method .
JAVA CODE
Composite parent = new Composite(table.getParent(), SWT.NONE);
parent.setLayout(new GridLayout(2, false));
Label label = new Label(parent, SWT.NULL);
label.setText("Name :");
System.out.println(table.getSelection()[0].getText());
Text text = new Text(parent, SWT.BORDER);
text.setText(table.getSelection()[0].getText());
text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL
| GridData.GRAB_HORIZONTAL));
ViewActionDelegate.resize(200, true, 20, table.getShell());
If i did like this on click action each time data will be appended to existing shell . How can I clear the righ hand side details ? . I posted a image below .
How can i do this ? .
Finally I found the answer .
Issue :
I append new elements into already created composite .
Solution:
Clear elements from existing composite before drawing new elements .
Java Code :
if ((composite != null) && (!composite.isDisposed())) {
Control[] children = composite2.getChildren();
for(Control ch : children){
ch.dispose();
}
}