ASE Home Page Products Download Purchase Support About ASE
ChartDirector General
Forum HomeForum Home   SearchSearch

Message ListMessage List     Post MessagePost Message

  simple SQLite/Python script
Posted by Bruce on Aug-19-2014 05:53
Attachments:
I think that sometimes people have a hard time getting started with integrating SQLite into ChartDirector.  Here's a simple script:

#!/usr/bin/python
from pychartdir import *
import sqlite3 as sql
con = sql.connect('test.db')

c = XYChart(600, 400)
title = c.addTitle("Test Chart 1", "timesbi.ttf", 18)
c.setPlotArea(40, 40, 540, 330)

data = list()
label = list()

with con:
  con.row_factory = sql.Row
  cur = con.cursor()
  cur.execute("select name, sum(data) as data from t1 group by name order by data asc")
  rows = cur.fetchall()
  for row in rows:
    data.append(row["data"])
    label.append(row["name"])

c.xAxis().setLabels(label)
c.addBarLayer(data)

c.makeChart("test.png")

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

That's the whole script, which uses mostly defaults.  Here's the db that I used:

[bruceg@localhost chartdir]$ sqlite3 test.db .dump
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE t1 (
id integer primary key,
name text not null,
data integer not null);
INSERT INTO "t1" VALUES(1,'n1',100);
INSERT INTO "t1" VALUES(2,'n1',200);
INSERT INTO "t1" VALUES(3,'n2',150);
INSERT INTO "t1" VALUES(4,'n2',175);
INSERT INTO "t1" VALUES(5,'n3',200);
INSERT INTO "t1" VALUES(6,'n3',100);
INSERT INTO "t1" VALUES(7,'n3',140);
INSERT INTO "t1" VALUES(8,'n3',140);
INSERT INTO "t1" VALUES(9,'n4',140);
COMMIT;

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

The resulting chart is attached.
test.png