Duplicate Values in X axis
Posted: Sat Nov 04, 2006 7:15 pm
I am using a DBChart to show integer values on the y axis and a month and year string on the x axis. I am displaying a line graph. Each line represents a company. I build each company's results a line at a time (see code below). The problem is that some of the months and years are being duplicated on the x axis and the lines are not in sync visually. Such that October 2006 appears twice for the companies that have data for October. Can you help? Is my mehodology of building the data in the chart correct?
The code is below:
Companys_Query2.Close;
Companys_Query2.SQL.Clear;
Companys_Query2.Sql.Add('Select Distinct Company, CompanyID from Companys');
Companys_Query2.ActiveRuntime := True;
DBChart1.RemoveAllSeries;
DBChart1.LeftAxis.Automatic := True;
if blRestrictPoints then
begin
DBChart1.MaxPointsPerPage := intMaxPointsPerPage;
end
else
begin
DBChart1.MaxPointsPerPage := 0;
end;
DBChart1.Legend.Title.Font.Name := 'Times New Roman';
DBChart1.Legend.Title.Font.Size := 8;
DBChart1.LeftAxis.LabelsFont.Name := 'Times New Roman';
DBChart1.LeftAxis.LabelsFont.Size := 8;
DBChart1.BottomAxis.LabelsFont.Name := 'Times New Roman';
DBChart1.BottomAxis.LabelsFont.Size := 8;
for intList := 1 to Companys_Query2.RecordCount do
begin
strCompany := Companys_Query2.FieldByName('Company').asString;
intCompanyID := Companys_Query2.FieldByName('CompanyID').asInteger;
TempResults_Query1 := TnxQuery.Create(self);
TempResults_Query1.Database := frmSearch.Extractor_Database1;
TempResults_Query1.SQL.Add('SELECT Case _month ');
TempResults_Query1.SQL.Add('when 1 then ' + '''January ''');
TempResults_Query1.SQL.Add('when 2 then ' + '''February ''');
TempResults_Query1.SQL.Add('when 3 then ' + '''March ''');
TempResults_Query1.SQL.Add('when 4 then ' + '''April ''');
TempResults_Query1.SQL.Add('when 5 then ' + '''May ''');
TempResults_Query1.SQL.Add('when 6 then ' + '''June ''');
TempResults_Query1.SQL.Add('when 7 then ' + '''July ''');
TempResults_Query1.SQL.Add('when 8 then ' + '''August ''');
TempResults_Query1.SQL.Add('when 9 then ' + '''September ''');
TempResults_Query1.SQL.Add('when 10 then ' + '''October ''');
TempResults_Query1.SQL.Add('when 11 then ' + '''November ''');
TempResults_Query1.SQL.Add('when 12 then ' + '''December ''');
TempResults_Query1.SQL.Add('end || Cast(_year as char(4)) as period, sum(entityhits) as total ');
TempResults_Query1.SQL.Add('from ');
TempResults_Query1.SQL.Add('(select extract(year from publicationdate) _year, ');
TempResults_Query1.SQL.Add('extract(month from publicationdate) _month, entityhits from ' + strExtractTableName + ' as B, PublisherCredibility as C ' );
TempResults_Query1.SQL.Add(' where B.PublisherID=C.PublisherID and B.ElementID =:intElementID and C.PublisherCredibility>=:dbPublisherCredibilityThreshold ');
TempResults_Query1.SQL.Add(' and B.CountryID=:CountryID ');
TempResults_Query1.SQL.Add(' order by _year, _month) t group by _year, _month;');
TempResults_Query1.ParamByName('intCompanyID').AsInteger := intCompanyID;
TempResults_Query1.ParamByName('CountryID').AsInteger := CountryID;
TempResults_Query1.ParamByName('dbPublisherCredibilityThreshold').AsFloat := dbPublisherCredibilityThreshold;
if TempResults_Query1.Prepared = False then
begin
TempResults_Query1.Prepare;
end;
TempResults_Query1.ActiveRuntime := True;
if TempResults_Query1.RecordCount > 0 then
begin
if TempResults_Query1.RecordCount = 1 then
begin
intList2 := intList2 + 1;
tmplineSeries := TLineSeries.Create(Self);
DBChart1.AddSeries(tmpLineSeries);
DBChart1.Legend.Visible := True;
DBChart1.Legend.CheckBoxes := True;
if blRestrictPoints then
begin
DBChart1.MaxPointsPerPage := intMaxPointsPerPage;
end
else
begin
DBChart1.MaxPointsPerPage := 0;
end;
With tmpLineSeries do
Begin
DataSource:= TempResults_Query1;
YValues.ValueSource := 'Total';
XLabelsSource := 'Period';
//XValues.DateTime := True;
Title := strElement;
case intList2 of
1 :Begin
tmpLineSeries.Color := $00408080;
end;
2 :Begin
tmpLineSeries.Color := $00A4C7A3;
end;
3 :Begin
tmpLineSeries.Color := $00804000;
end;
4 :Begin
tmpLineSeries.Color := $004080FF;
end;
5 :Begin
tmpLineSeries.Color := $00400080;
end;
6 :Begin
tmpLineSeries.Color := $00C08080;
end;
7 :Begin
tmpLineSeries.Color := $00B3FFFF;
end;
8 :Begin
tmpLineSeries.Color := clAqua;
end;
9 :Begin
tmpLineSeries.Color := $00FF8000;
end;
10:Begin
tmpLineSeries.Color := $00A4C7A3;
end;
end;
The code is below:
Companys_Query2.Close;
Companys_Query2.SQL.Clear;
Companys_Query2.Sql.Add('Select Distinct Company, CompanyID from Companys');
Companys_Query2.ActiveRuntime := True;
DBChart1.RemoveAllSeries;
DBChart1.LeftAxis.Automatic := True;
if blRestrictPoints then
begin
DBChart1.MaxPointsPerPage := intMaxPointsPerPage;
end
else
begin
DBChart1.MaxPointsPerPage := 0;
end;
DBChart1.Legend.Title.Font.Name := 'Times New Roman';
DBChart1.Legend.Title.Font.Size := 8;
DBChart1.LeftAxis.LabelsFont.Name := 'Times New Roman';
DBChart1.LeftAxis.LabelsFont.Size := 8;
DBChart1.BottomAxis.LabelsFont.Name := 'Times New Roman';
DBChart1.BottomAxis.LabelsFont.Size := 8;
for intList := 1 to Companys_Query2.RecordCount do
begin
strCompany := Companys_Query2.FieldByName('Company').asString;
intCompanyID := Companys_Query2.FieldByName('CompanyID').asInteger;
TempResults_Query1 := TnxQuery.Create(self);
TempResults_Query1.Database := frmSearch.Extractor_Database1;
TempResults_Query1.SQL.Add('SELECT Case _month ');
TempResults_Query1.SQL.Add('when 1 then ' + '''January ''');
TempResults_Query1.SQL.Add('when 2 then ' + '''February ''');
TempResults_Query1.SQL.Add('when 3 then ' + '''March ''');
TempResults_Query1.SQL.Add('when 4 then ' + '''April ''');
TempResults_Query1.SQL.Add('when 5 then ' + '''May ''');
TempResults_Query1.SQL.Add('when 6 then ' + '''June ''');
TempResults_Query1.SQL.Add('when 7 then ' + '''July ''');
TempResults_Query1.SQL.Add('when 8 then ' + '''August ''');
TempResults_Query1.SQL.Add('when 9 then ' + '''September ''');
TempResults_Query1.SQL.Add('when 10 then ' + '''October ''');
TempResults_Query1.SQL.Add('when 11 then ' + '''November ''');
TempResults_Query1.SQL.Add('when 12 then ' + '''December ''');
TempResults_Query1.SQL.Add('end || Cast(_year as char(4)) as period, sum(entityhits) as total ');
TempResults_Query1.SQL.Add('from ');
TempResults_Query1.SQL.Add('(select extract(year from publicationdate) _year, ');
TempResults_Query1.SQL.Add('extract(month from publicationdate) _month, entityhits from ' + strExtractTableName + ' as B, PublisherCredibility as C ' );
TempResults_Query1.SQL.Add(' where B.PublisherID=C.PublisherID and B.ElementID =:intElementID and C.PublisherCredibility>=:dbPublisherCredibilityThreshold ');
TempResults_Query1.SQL.Add(' and B.CountryID=:CountryID ');
TempResults_Query1.SQL.Add(' order by _year, _month) t group by _year, _month;');
TempResults_Query1.ParamByName('intCompanyID').AsInteger := intCompanyID;
TempResults_Query1.ParamByName('CountryID').AsInteger := CountryID;
TempResults_Query1.ParamByName('dbPublisherCredibilityThreshold').AsFloat := dbPublisherCredibilityThreshold;
if TempResults_Query1.Prepared = False then
begin
TempResults_Query1.Prepare;
end;
TempResults_Query1.ActiveRuntime := True;
if TempResults_Query1.RecordCount > 0 then
begin
if TempResults_Query1.RecordCount = 1 then
begin
intList2 := intList2 + 1;
tmplineSeries := TLineSeries.Create(Self);
DBChart1.AddSeries(tmpLineSeries);
DBChart1.Legend.Visible := True;
DBChart1.Legend.CheckBoxes := True;
if blRestrictPoints then
begin
DBChart1.MaxPointsPerPage := intMaxPointsPerPage;
end
else
begin
DBChart1.MaxPointsPerPage := 0;
end;
With tmpLineSeries do
Begin
DataSource:= TempResults_Query1;
YValues.ValueSource := 'Total';
XLabelsSource := 'Period';
//XValues.DateTime := True;
Title := strElement;
case intList2 of
1 :Begin
tmpLineSeries.Color := $00408080;
end;
2 :Begin
tmpLineSeries.Color := $00A4C7A3;
end;
3 :Begin
tmpLineSeries.Color := $00804000;
end;
4 :Begin
tmpLineSeries.Color := $004080FF;
end;
5 :Begin
tmpLineSeries.Color := $00400080;
end;
6 :Begin
tmpLineSeries.Color := $00C08080;
end;
7 :Begin
tmpLineSeries.Color := $00B3FFFF;
end;
8 :Begin
tmpLineSeries.Color := clAqua;
end;
9 :Begin
tmpLineSeries.Color := $00FF8000;
end;
10:Begin
tmpLineSeries.Color := $00A4C7A3;
end;
end;