Customizing the X axis

Using a variety of different tools and configurations, you can fully customize rendering of the X-axis.

First, review the complete list of available parameters to see if you can accomplish your desired customization by adjusting a flag. A complete list can be found at CIQ.ChartEngine.XAxis

Colors and fonts

These can be controlled by manipulating the CSS styles associated with the x-axis.

You can override the stx_xaxis class in stx-chart.css to change the font and the color. Remember that if you are using the default dark theme you will need to override the defaults for .ciq-night .stx_xaxis style.

Fully customized X-Axis

Option 1:

Set CIQ.ChartEngine.XAxis#formatter to a function which will override the x-axis labels to anything you want.

Option 2:

Use the locale/internationalizer to format dates.

Examples:

CIQ.I18N.setLocale(stxx, "en"); // set localization services -- before any UI or chart initialization is done
// override date formatting of the introday hover label to add year
stxx.internationalizer.monthDay=new Intl.DateTimeFormat(this.locale, {year:"numeric", month:"numeric", day:"numeric"});

// override time formatting on both the hover and the x axis s labels to enable 12 hour clock (hour12:true)
stxx.internationalizer.hourMinute=new Intl.DateTimeFormat(this.locale, {hour:"numeric", minute:"numeric", hour12:true});
stxx.internationalizer.hourMinuteSecond=new Intl.DateTimeFormat(this.locale, {hour:"numeric", minute:"numeric", second:"numeric", hour12:true});

// override time formatting to enable 12 hour clock (hour12:true)
stxx.internationalizer.hourMinute=new Intl.DateTimeFormat(this.locale, {hour:"numeric", minute:"numeric", hour12:true});
stxx.internationalizer.hourMinuteSecond=new Intl.DateTimeFormat(this.locale, {hour:"numeric", minute:"numeric", second:"numeric", hour12:true});

More details on localization can be found in the Localization tutorial.

Option 3:

Use an API injection on createXAxis() to totally override all of the default functionality. This is an advanced feature and only recommended for someone very experienced with the library.

CIQ.ChartEngine.prototype.prepend("createXAxis",function(chart){
    // add your logic to create your own X-Axis. We can help...
     //return axisRepresentation; // This is your array of CIQ.ChartEngine.XAxisLabel() elements used by CIQ.ChartEngine.drawXAxis()
});

See CIQ.ChartEngine.AdvancedInjectable#createXAxis for function signature and other details.

The injection should retun an axisRepresentation array (normally created internally by createXAxis() ). drawXAxis() uses this array to draw the axis. Your injection can put anything you want to see in the x-axis labels (text). Each entry should specify the type of label (boundary or line) and the pixel location where you want to set it (hz). This is done by pushing CIQ.ChartEngine.XAxisLabel() elements into the axisRepresentation array.

See CIQ.ChartEngine.XAxisLabel for additional details on format and requirements.

For instance, call either:

axisRepresentation.push(new CIQ.ChartEngine.XAxisLabel(hz,"boundary",text));

or

axisRepresentation.push(new CIQ.ChartEngine.XAxisLabel(hz,"line",text));

Your injection will also need to create the chart.xaxis array which is used by the HUD (crosshairs) as you slide the mouse through the chart.

This array takes objects of the following format:

    var obj={
    DT: current date,                    // in js date format
    Date: CIQ.yyyymmddhhmm(dtShifted)     // in string format
};

For each item added to the xaxisRepresentation array, add a corresponding item to chart.xaxis like so:

chart.xaxis.push(obj);

Option 4:

Allow the default axisRepresentation array to be created, but intercept it and adjust it before it is used. For this you can use an API injection similar to this:

CIQ.ChartEngine.prototype.prepend("createXAxis",function(chart){
        axisRepresentation = this.createTickXAxisWithDates(chart);
        // add you logic here to adjust the default axisRepresentation array as needed
        // iterate trough each element and adjust labels(text) or horizontal location (hz)
        return axisRepresentation;
});

More granular x-axis

CIQ.ChartEngine#createTickXAxisWithDates is the method called by CIQ.ChartEngine.AdvancedInjectable#createXAxis to make all of the axisRepresentation calculations. It is algorithmically designed to create an x-axis that is responsive to various amounts of user panning, zooming, and periodicity selection. Depending on those factors, it will print different dates or times formats, will prevent label overlaps, and will evenly space labels. It will do this by finding the closest match in a pre-set timeInterval map, which contains allowable label values. (This algorithm will only be used if timeUnit and timeUnitMultiplier are not set).

The map contains an entry for every time unit that can be display (second, minute, hour, etc). Each map entry contains:

                arr: list of possible timeUnit Multipliers
                minTimeUnit: lower range for the interval
                maxTimeUnit: upper range for the interval

The default map is as follows, but you can override this as well:

                stxx.timeIntervalMap={};
                stxx.timeIntervalMap[CIQ.MILLISECOND]={
                    // a label will be attempted to be placed every
                    // 1,2,5,10,20,50,100,250 or 500 MILLISECONDS
                    // as space permits.
                    arr: [1,2,5,10,20,50,100,250,500],
                    minTimeUnit:0,
                    maxTimeUnit:1000
                };
                stxx.timeIntervalMap[CIQ.SECOND]={
                    arr: [1, 2, 5, 10,15,30],
                    minTimeUnit: 0,
                    maxTimeUnit: 60
                };
                stxx.timeIntervalMap[CIQ.MINUTE]={
                    arr: [1,2,5,10,15,30],
                    minTimeUnit: 0,
                    maxTimeUnit: 60
                };
                stxx.timeIntervalMap[CIQ.HOUR]={
                    arr: [1, 2, 3, 4,6,12],
                    minTimeUnit: 0,
                    maxTimeUnit: 24
                };
                stxx.timeIntervalMap[CIQ.DAY]={
                    arr: [1,2,7,14],
                    minTimeUnit: 1,
                    maxTimeUnit: 32
                };
                stxx.timeIntervalMap[CIQ.MONTH]={
                    arr: [1,2,3,6],
                    minTimeUnit:1,
                    maxTimeUnit:13
                };
                stxx.timeIntervalMap[CIQ.YEAR]={
                    arr: [1,2,3,5],
                    minTimeUnit:1,
                    maxTimeUnit:20000000
                };
                stxx.timeIntervalMap[CIQ.DECADE]={
                    arr: [10],
                    minTimeUnit: 0,
                    maxTimeUnit: 2000000
                };
            }

The timeUnit multipliers will be used in the order in which they appear as long as the resulting label date is within the minTimeUnit and maxTimeUnit range.

For example, if the chart is currently displaying a zoom level where day labels should be displayed as determined by time differential and distance between bars, the following entry on the map will be activated:

                stxx.timeIntervalMap[CIQ.DAY]={
                    arr: [1,2,7,14],
                    minTimeUnit: 1,
                    maxTimeUnit: 32
                };

The algorithm will first attempt to add a label every 1 day. If there is not enough room, it will attempt every 2 days and so on. Once the correct multiplier is found, that multiplier will be used until maxTimeUnit is reached. So if there is only room to fit daily labels every 7 days, you will see them on dates that are multiples of 7. i.e. there will be a label on :

  • 7th of the month
  • 14th or the month
  • 21st of the month
  • 28th of the month

Assuming those dates match existing bars - otherwise they will be skipped.

Example for overriding the default algorithm with hard coded values:

stxx.chart.xAxis.timeUnit = CIQ.DAY;
stxx.chart.xAxis.timeUnitMultiplier = 7;

This kind of hard coded approach is only recommended for static charts because you will need to manually adjust your override as your periodicity changes to maintain a reasonable interval between tags.

Rendering

Once label spacing has been calculated, the rendering process will begin. Just because a specific spacing has been set, doesn't mean there will be enough space to use it. When in tick periodicity, for example, since tick charts do not have a constant time interval, there sometimes won't be enough ticks to create enough space for a label to display, so the label will be skipped at rendering time. To address this, you can try changing the zoom level by setting a smaller span and thereby creating more space between the ticks ( see CIQ.ChartEngine#setSpan and Managing Chart Zoom and Range. You can also try to change the xaxis label width settings to allow less whitespace between the labels using CIQ.ChartEngine.XAxis#minimumLabelWidth

Example:

stxx.chart.xAxis.minimumLabelWidth=20;