~mortenoh/+junk/dhis2-detailed-import-export

« back to all changes in this revision

Viewing changes to dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/javascript/history.js

  • Committer: larshelge at gmail
  • Date: 2009-03-03 16:46:36 UTC
  • Revision ID: larshelge@gmail.com-20090303164636-2sjlrquo7ib1gf7r
Initial check-in

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
function createChart()
 
3
{
 
4
    var canvas = document.getElementById( 'canvas' );
 
5
    var width = parseInt( canvas.style.width ) - 1;
 
6
    var height = parseInt( canvas.style.height ) - 1;
 
7
    var verticalPadding = height * 0.02;
 
8
    var horzontalPadding = verticalPadding;
 
9
    var cellWidth = width / historyLength;
 
10
    var fontSizePixels = 10;
 
11
    var lineHeight = 4; // ?
 
12
    var barBase = Math.round( height - verticalPadding * 2 - fontSizePixels * 2 - lineHeight );
 
13
    var maxBarHeight = barBase - verticalPadding * 2 - fontSizePixels;
 
14
    var barWidth = cellWidth * 0.4;
 
15
    var barPaddingLeft = ( cellWidth - barWidth ) / 2;
 
16
    var barColor = '#0000ff';
 
17
    var barBaseColor = '#808080';
 
18
    var textColor = '#000000';
 
19
    var minLimitColor = '#008000';
 
20
    var maxLimitColor = '#ff0000';
 
21
    var averageColor = '#0080ff';
 
22
 
 
23
    var g = new jsGraphics( 'canvas' );
 
24
    g.setFont( 'sans-serif', fontSizePixels + 'px', Font.PLAIN );
 
25
    g.setStroke( 2 );
 
26
 
 
27
    g.setColor( barColor );
 
28
    var barHeight;
 
29
 
 
30
    for ( i = 0; i < historyLength; ++i )
 
31
    {
 
32
        if ( historyPoints[i][1] )
 
33
        {
 
34
            barHeight = Math.round( maxBarHeight * historyPoints[i][1] / maxValue );
 
35
            g.fillRect( cellWidth * i + barPaddingLeft, barBase - barHeight, barWidth, barHeight );
 
36
        }
 
37
    }
 
38
 
 
39
    g.setColor( averageColor );
 
40
    var previousPoint = barBase - Math.round( maxBarHeight * historyPoints[0][2] / maxValue );
 
41
    var nextPoint;
 
42
 
 
43
    for ( i = 1; i < historyLength; ++i )
 
44
    {
 
45
        nextPoint = barBase - Math.round( maxBarHeight * historyPoints[i][2] / maxValue );
 
46
        g.drawLine( cellWidth * ( i - 1 ) + cellWidth / 2, previousPoint, cellWidth * i + cellWidth / 2, nextPoint );
 
47
        previousPoint = nextPoint;
 
48
    }
 
49
 
 
50
    g.setColor( barBaseColor );
 
51
    g.drawLine( 0, barBase, width, barBase );
 
52
 
 
53
    if ( minLimit )
 
54
    {
 
55
        var minLimitPos = barBase - maxBarHeight * minLimit / maxValue;
 
56
        g.setColor( minLimitColor );
 
57
        g.drawLine( 0, minLimitPos, width, minLimitPos );
 
58
    }
 
59
 
 
60
    if ( maxLimit )
 
61
    {
 
62
        var maxLimitPos = barBase - maxBarHeight * maxLimit / maxValue;
 
63
        g.setColor( maxLimitColor );
 
64
        g.drawLine( 0, maxLimitPos, width, maxLimitPos );
 
65
    }
 
66
 
 
67
    g.setColor( textColor );
 
68
 
 
69
    for ( i = 0; i < historyLength; ++i )
 
70
    {
 
71
        if ( historyPoints[i][1] )
 
72
        {
 
73
            barHeight = Math.round( maxBarHeight * historyPoints[i][1] / maxValue );
 
74
                g.drawStringRect( historyPoints[i][1].toFixed(), cellWidth * ( i - 1 ) + cellWidth / 2, barBase - barHeight - verticalPadding - fontSizePixels, cellWidth * 2, 'center' );
 
75
        }
 
76
 
 
77
        g.drawStringRect( historyPoints[i][0], cellWidth * i, barBase + verticalPadding, cellWidth, 'center' );
 
78
    }
 
79
 
 
80
    g.paint();
 
81
}
 
82
 
 
83
function saveMinLimit( organisationUnitId, dataElementId )
 
84
{
 
85
    var minLimitField = document.getElementById( "minLimit" );
 
86
    var maxLimitField = document.getElementById( "maxLimit" );
 
87
 
 
88
    var request = new Request();
 
89
    request.setCallbackSuccess( refreshWindow );
 
90
    request.setCallbackError( refreshWindow );
 
91
 
 
92
    if ( minLimitField.value == '' )
 
93
    {
 
94
        request.send( 'removeMinMaxLimits.action?organisationUnitId=' + organisationUnitId + '&dataElementId=' + dataElementId );
 
95
    }
 
96
    else
 
97
    {
 
98
        var minLimit = Number( minLimitField.value );
 
99
        var maxLimit = Number( maxLimitField.value );
 
100
        
 
101
        if ( minLimit )
 
102
        {
 
103
                if ( minLimit < 0 )
 
104
                {
 
105
                    minLimit = 0;
 
106
                }
 
107
 
 
108
            if ( !maxLimit || maxLimit <= minLimit )
 
109
            {
 
110
                maxLimit = minLimit + 1;
 
111
            }
 
112
 
 
113
            request.send( 'saveMinMaxLimits.action?organisationUnitId=' + organisationUnitId + '&dataElementId=' + dataElementId + '&minLimit=' + minLimit + '&maxLimit=' + maxLimit );
 
114
        }
 
115
        else
 
116
        {
 
117
            refreshWindow();
 
118
        }
 
119
    }
 
120
}
 
121
 
 
122
function saveMaxLimit( organisationUnitId, dataElementId )
 
123
{
 
124
    var minLimitField = document.getElementById( "minLimit" );
 
125
    var maxLimitField = document.getElementById( "maxLimit" );
 
126
 
 
127
    var request = new Request();
 
128
    request.setCallbackSuccess( refreshWindow );
 
129
    request.setCallbackError( refreshWindow );
 
130
 
 
131
    if ( maxLimitField.value == '' )
 
132
    {
 
133
        request.send( 'removeMinMaxLimits.action?organisationUnitId=' + organisationUnitId + '&dataElementId=' + dataElementId );
 
134
    }
 
135
    else
 
136
    {
 
137
        var minLimit = Number( minLimitField.value );
 
138
        var maxLimit = Number( maxLimitField.value );
 
139
        
 
140
        if ( maxLimit )
 
141
        {
 
142
            if ( maxLimit < 1 )
 
143
            {
 
144
                maxLimit = 1;
 
145
            }
 
146
 
 
147
            if ( !minLimit )
 
148
            {
 
149
                minLimit = 0;
 
150
            }
 
151
            else if ( minLimit >= maxLimit )
 
152
            {
 
153
                minLimit = maxLimit - 1;
 
154
            }
 
155
 
 
156
            request.send( 'saveMinMaxLimits.action?organisationUnitId=' + organisationUnitId + '&dataElementId=' + dataElementId + '&minLimit=' + minLimit + '&maxLimit=' + maxLimit );
 
157
        }
 
158
        else
 
159
        {
 
160
            refreshWindow();
 
161
        }
 
162
    }
 
163
}
 
164
 
 
165
function refreshWindow()
 
166
{
 
167
    window.location.reload();
 
168
}