How to clean an AnnotatedTimeline gwt visualization chart - gwt

I am dealing with a problem with annotatedtimelines.
I have to draw some charts depending on the tab that the user is, so when the tab is changed I clean the current chart and draw the new data. But how to do that?
For now I am removing all the rows, but it is not working.
Can someone help me?
here is the code:
...
//Creating Columns
dataTable.addColumn(ColumnType.DATETIME, "Time");
dataTable.addColumn(ColumnType.NUMBER, "Realtime Consumption");
dataTable.addColumn(ColumnType.NUMBER, "Historical Consumption");
//Create options
options.setDisplayAnnotations(false);
options.setDisplayZoomButtons(false);
options.setScaleType(AnnotatedTimeLine.ScaleType.FIXED);
options.setLegendPosition(AnnotatedTimeLine.AnnotatedLegendPosition.SAME_ROW);
options.setAllowRedraw(true);
options.setDisplayRangeSelector(false);
options.setFill(30);
//to parse the time
DateTimeFormat dtf = DateTimeFormat.getFormat("hh:mm:ss");
//For each item of the list
for(int i = 0; i < list.size(); i++){
//get date
Date date = new Date(list.getTimeAt(i));
//get hh:mm:ss
String time = date.getHours()+":"+date.getMinutes()+":"+date.getSeconds();
//add row
dataTable.addRow();
dataTable.setValue(dataTable.getNumberOfRows() - 1, 0, dtf.parse(time));
dataTable.setValue(dataTable.getNumberOfRows() - 1, 2, list.getDataAt(i));
}
/**
* To clean the chart
*/
public void cleanChart(){
//Remove all rows
this.dataTable.removeRows(0, dataTable.getNumberOfRows());
//Redraw the chart
this.draw(this.dataTable, this.options);
}
Thanks,
MaurĂ­cio
It is very strange, but I did it (that seems ok, because I'd create another datatable), and it still not working. The old data continues there.
I have a class that extends AnnotatedTimeLine, and the clean method is now:
/**
* Method to clean the annotated time line
*/
public void clean() {
//Create new table
this.dataTable = DataTable.create();
//Create columns
this.dataTable.addColumn(ColumnType.DATETIME, "Time");
this.dataTable.addColumn(ColumnType.NUMBER, "Data 1");
this.dataTable.addColumn(ColumnType.NUMBER, "Data 2");
//Redraw the chart with the same options
this.draw(this.dataTable, this.options);
}
Any idea?
Thanks!

You need to recreate DataTable for your new data and don't need to do
this.dataTable.removeRows(0, dataTable.getNumberOfRows());
I have something like this
public void reloadChart(List<SharedBean> list, String titleX) {
viz.draw(getChartData(), getOptions(titleX));
}
private DataTable getChartData() {
DataTable data = DataTable.create();
data.addColumn(ColumnType.STRING, "column");
data.addColumn(ColumnType.NUMBER, "value");
data.addRows(list.size());
int row = 0;
int i = 0;
for(SharedBean bean: list){
......
}
return data;
}

Related

In GXT 3 how to access another field in same row during handling of onCompleteEdit event

I have a GridInlineEditing object for GXT 3.0 grid. It all works as expected - I can capture user editing events and find out the row, column and model/change-record for this event.
What I have not figured out is how to best access another control in that row.
Specifically, I have this Column model:
private void initializeColumnModel() {
// Create the configurations for each column in the grid
List<ColumnConfig<Reminder, ?>> ccs = new LinkedList<ColumnConfig<Reminder, ?>>();
typeColumnConfig = new ColumnConfig<Reminder,String>( properties.name(), 120, "Type" );
completedColumnConfig = getDateCellColumn( properties.completed_(), 200, "Completed" );
dueColumnConfig = getDateCellColumn( properties.due_(), 200, "Due" );
applicableColumnConfig = new ColumnConfig<Reminder,Boolean>( properties.applicable(), 140, "Applicable");
// Add column configurations to ColumnModel.
ccs.add(typeColumnConfig);
ccs.add(completedColumnConfig);
ccs.add(dueColumnConfig);
ccs.add(applicableColumnConfig);
applicableColumnConfig.setAlignment(HasHorizontalAlignment.ALIGN_CENTER);
typeColumnConfig.setAlignment(HasHorizontalAlignment.ALIGN_CENTER);
reminderColumnModel = new ColumnModel<Reminder>(ccs);
}
and am attempting to intercept a change to the 'completed date' value and then programmatically operate the 'due date' control (adding a fixed number of years to that as if the user just did it).
Here's how I capture the event:
#Override
public Widget asWidget() {
if(!gridInitialized){
editing = new GridInlineEditing<Reminder>(grid);
DateField dueDateField = getDateField();
DateField completedDateField = getDateField();
editing.addEditor(dueColumnConfig, dueDateField);
editing.addEditor(completedColumnConfig, completedDateField);
editing.addEditor(applicableColumnConfig, new CheckBox());
editing.addCompleteEditHandler(new CompleteEditHandler<Reminder>(){
#Override
public void onCompleteEdit(CompleteEditEvent<Reminder> event) {
GridCell cell = event.getEditCell();
int row = cell.getRow();
int col = cell.getCol();
Reminder rem = reminderStore.get(row);
Store<Reminder>.Record rec = reminderStore.getRecord(rem);
//System.out.println("row:"+row+", col:"+col+", applic:"+rem.getApplicable());
//System.out.println("rec:"+rec.toString());
Change<Reminder, Boolean> applicChange = rec.getChange(properties.applicable());
Change<Reminder, Date> dueChange = rec.getChange(properties.due_());
Change<Reminder, Date> comChange = rec.getChange(properties.completed_());
System.err.print("Row "+(row+1)+" changed: ");
if(applicChange!=null){
boolean applicValue = applicChange.getValue();
System.out.println("applicable changed to "+applicValue);
}
if(dueChange!=null){
Date dueValue = dueChange.getValue();
System.out.println("due changed to: "+SimpleDate.convertFromDate(dueValue));
}
if(comChange!=null){
Date comValue = comChange.getValue();
System.out.println("com changed to: "+SimpleDate.convertFromDate(comValue));
try{
fixDueDate(row,comValue, rem);
}
catch(Exception e){
//boo.
System.err.println("Could not update the due date for this completion date change.");
}
}
}
});
gridInitialized=true;
}
// Initialize the Revert Changes button.
revert.addSelectHandler(new SelectHandler(){
#Override
public void onSelect(SelectEvent event) {
reminderStore.rejectChanges();
}
});
return widget;
}
The method 'fixDueDate' is where I hope to place my logic that adjusts the other column control (another DateField):
private void fixDueDate(int row, Date completedDate, Reminder rem) throws InvalidDateFormatException, InvalidDateException{
SimpleDate newCompDate = new SimpleDate(completedDate);
SimpleDate dueDate = newCompDate.addYears(rem.getRenewalYears());
//rem.setDue(dueDate.getFormattedDate());
//reminderStore.update(rem);
}
thanks.
This works:
private void fixDueDate(int row, Date completedDate, Reminder rem, Store<Reminder>.Record rec )
throws InvalidDateFormatException, InvalidDateException{
SimpleDate newCompDate = new SimpleDate(completedDate);
SimpleDate dueDate = newCompDate.addYears(rem.getRenewalYears());
rec.addChange(dueColumnConfig.getValueProvider(), dueDate.toDate());
}
and the modified field marks appear on both the user modified field and the field modified by this rec.addChange call above.

PdfPageEventHelper in iTextSharp

sorry if this was posted earlier
I need to print list of expenses in a pdf document. the list may extend to any number of pages.
i'm writing the list by iterating the datarow object. One important thing is whenever the current page is going to end need to print the running total at the end of the page. i've wrote a class that implements PdfPageEventHandler as below
public class PaymentPageEventHandler : iTextSharp.text.pdf.PdfPageEventHelper
{
PdfContentByte cb;
private string subTotal = "";
public string GetSubtotal
{
set{
value = subTotal;
}
get
{
return subTotal;
}
}
public override void OnEndPage(PdfWriter writer, Document document)
{
// base.OnEndPage(writer, document);
cb = writer.DirectContent;
float[] iOuterTblWidth = { 10, 40F, 8, 12, 10 }; column widths of the table.
PdfPTable pdftbl = new PdfPTable(iOuterTblWidth);
PdfPCell cell = new PdfPCell();
cell.Colspan = 4;
cell.AddElement(new Chunk("Sub-Total"));
cell.HorizontalAlignment = 0;
pdftbl.AddCell(cell);
cell = new PdfPCell();
cell.AddElement(new Chunk(GetSubtotal));
cell.HorizontalAlignment = 2;
pdftbl.AddCell(cell);
}
}
the onEndPAge event above tries to write two columns. the above code gets called but the rows are not appearing in the pdf page.
I'm calling the above class like below
PaymentPageEventHandler ppem = new PaymentPageEventHandler();
ppem.GetSubtotal = "123"; // test value to print as running total
writer.PageEvent = ppem; // assigning event to writer object
Should i call explicity the PdfContentByte variable cb to write. if so how should i write the cells to the pdf.
can any one help me out on this.
You need to add the table using the WriteSelectedRows method. Please read the documentation and take a look at this example (or the C# ports of the examples)
Look for the line:
table.WriteSelectedRows(0, -1, 34, 803, writer.DirectContent);
Obviously, you'll need to adapt the coordinates.

GWT CellTable Data Not Displaying

I have the following code in my modules onModuleLoad() method:
List<MyPromo> promotionData = new ArrayList<MyPromo>();
MyPromo promotion1 = new MyPromo(...);
promotionData.add(promotion1);
PromotionTable<MyPromo> promoTable = new PromotionTable<MyPromo>(tableColumns, promotionData);
and
public class PromotionTable <T extends Promotion> extends CellTable<T>{
public PromotionTable(List<ColumnGroup<T>> columns, List<T> data) {
super();
this.setWidth("100%");
this.setHeight("500px");
this.setHeaderBuilder(new PromotionTableHeaderBuilder(columns, this));
this.setFooterBuilder(new PromotionTableFooterBuilder(this));
ListDataProvider<T> dataProvider = new ListDataProvider<T>();
dataProvider.setList(data);
dataProvider.addDataDisplay(this);
}
...
The columns for the CellTable just take properties off the MyPromo object and return a String value to display. However, nothing is displayed in the table, just the column headers. Any idea why this is?
The problem is with the following code
ListDataProvider<T> dataProvider = new ListDataProvider<T>();
dataProvider.setList(data);
dataProvider.addDataDisplay(this);
By the time you do setList, ListDataProvider must know its consumers ie displays (Refer setList implementaion).
So, the order of the code is wrong. First do addDataDisplay and then do setList. It will work.
I was constructing the dataProvider and assigning it to the celltable (or mu extension of) in the constructor. It didn't like this for some reason, when I moved it out it worked.
Here is a simple example of CellTable, SimplePager and ListDataProvider.
CellTable<AlarmDisplayBTO> cellTable= new CellTable<AlarmDisplayBTO>();
TextColumn<AlarmDisplayBTO> dateColumn = new TextColumn<AlarmDisplayBTO>() {
#Override
public String getValue(AlarmDisplayBTO object) {
return object.getDate();
}
};
cellTable.addColumn(dateColumn, "Date");
TextColumn<AlarmDisplayBTO> alarmNameColumn = new TextColumn<AlarmDisplayBTO>() {
#Override
public String getValue(AlarmDisplayBTO object) {
return object.getAlarmName();
}
};
cellTable.addColumn(alarmNameColumn, "Alarm Name");
cellTable.setRowCount(alarmList.size());
// alarmList is an ArrayList<AlarmDisplayBTO> rendered from RPC call
cellTable.setRowData(0, alarmList);
cellTable.setEmptyTableWidget(new Label(" No Records Found"));
ListDataProvider<AlarmDisplayBTO> dataProvider = new ListDataProvider<AlarmDisplayBTO>();
dataProvider.addDataDisplay(cellTable);
dataProvider.setList(alarmList);
SimplePager pager = new SimplePager();
pager.setDisplay(cellTable);
pager.setPageSize(20); // 20 rows will be shown at a time
VerticalPanel vPanel = new VerticalPanel();
vPanel.add(cellTable);
vPanel.add(pager);
setWidget(new ScrollPanel(vPanel));
Hope this may help..

GWT dynamic width of linechart

I have putted below code line chart visualization.can any one tell me how to set the dynamic widht in linechart of gwt vissualization graph?
options.setWidth(1000); i want the instead of 1000 some dynamic value.
enter code here
public class CPUChart extends VerticalPanel{
private LineChart lineChart;
private DataTable data;
public CPUChart(final CPUChartDataQueue cpuChartDataQueue) {
VisualizationUtils.loadVisualizationApi(new Runnable() {
public void run() {
setLayoutData(new FitLayout());
lineChart = new LineChart(createTable(cpuChartDataQueue), createOptions("CPU Chart"));
add(lineChart);
}}, LineChart.PACKAGE);
}
private Options createOptions(String title) {
Options options = Options.create();
**options.setWidth(1000);
options.setHeight(300);**
options.setTitleFontSize(11);
options.setShowCategories(true);
options.setLegend(LegendPosition.BOTTOM);
options.setTitle(title);
options.setSmoothLine(true);
options.setAxisFontSize(12);
options.setColors(Color3D.create("#A4C735", "#4F7512"),Color3D.create("#FF4C4C", "#FF0303"));
options.setMin(0);
options.setMax(100);
options.setTitleY("Percentage(%)");
options.setTitleX("Time");
return options;
}
private AbstractDataTable createTable(CPUChartDataQueue cpuChartDataQueue) {
DataTable data = DataTable.create();
data.addColumn(ColumnType.STRING, "Time");
data.addColumn(ColumnType.NUMBER, "CPU Usage Percentage");
Queue<CPUChartData> chartDatas = cpuChartDataQueue.getQue();
if(chartDatas!=null && chartDatas.size()>0){
data.addRows(chartDatas.size());
int i=0;
for (CPUChartData chartData : chartDatas) {
data.setValue(i, 0, chartData.getLable());
data.setValue(i, 1, Integer.parseInt(chartData.getValue()));
i++;
}
}
return data;
}
public void refreshChart(CPUChartDataQueue cpuChartDataQueue){
data = DataTable.create();
data.addColumn(ColumnType.STRING, "Time");
data.addColumn(ColumnType.NUMBER, "CPU Usage Percentage");
Queue<CPUChartData> chartDatas = cpuChartDataQueue.getQue();
if(chartDatas!=null && chartDatas.size()>0){
data.addRows(chartDatas.size());
int i=0;
for (CPUChartData chartData : chartDatas) {
data.setValue(i, 0, chartData.getLable());
data.setValue(i, 1, Integer.parseInt(chartData.getValue()));
i++;
}
}
lineChart.draw(data,createOptions("CPU Chart"));
}
}
Most Google visualization charts require explicit sizes in pixel. Only some FLASH charts support percentage dimensions.
A workaround is to add a onResize Handler to the window and redraw/recreate the chart whenever the browser window is resized.

Setting ListGrid selection in SmartGWT with method "selectRecords(Record record)"

I'm trying to set the selected records of a ListGrid table object in SmartGWT using records, but I can't find any way of doing it. I want to select with record, not index. I want to use selectRecord(Record record) method.
As an example;
public void onModuleLoad()
{
VLayout main = new VLayout();
final ListGrid grid = new ListGrid();
grid.setHeight(500);
grid.setWidth(400);
grid.setFields(new ListGridField("name", "Name"));
grid.setData(createRecords());
final IButton button = new IButton("Select some");
button.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event)
{
grid.selectRecord(createRecord("orange"));
}
});
main.addMember(grid);
main.addMember(button);
RootPanel.get().add(main);
}
private ListGridRecord[] createRecords()
{
return new ListGridRecord[]{
createRecord("monkey"),
createRecord("banana"),
createRecord("orange"),
createRecord("sun")
};
}
private ListGridRecord createRecord(String name)
{
ListGridRecord record = new ListGridRecord();
record.setAttribute("name", name);
return record;
}
In this case I want to select orange, But this code select anything.
Is it posible? If possible how?
Thanks in advance.
Found this solution;
selectRecord(grid.getRecordList().find("name", "orange"));
There's a problem with your code:
When you write
grid.selectRecord(record);
it goes to search the same record instance that the grid has. If both instances of record are equal, only then & then it selects the record. Otherwise nothing happens as you're facing right now. Here what you need to do is:
ListGridRecord[] records = countryGrid.getRecords();
int i;
for (i = 0; i < records.length; i++)
{
if (records[i].getAttribute("name").equalsIgnoreCase("orange"))
{
break;
}
}
countryGrid.selectRecord(i);