/*
 * Licensed Materials - Property of Perforce Software, Inc. 
 * © Copyright Perforce Software, Inc. 2014, 2021 
 * © Copyright IBM Corp. 2009, 2014
 * © Copyright ILOG 1996, 2009
 * All Rights Reserved.
 *
 * Note to U.S. Government Users Restricted Rights:
 * The Software and Documentation were developed at private expense and
 * are "Commercial Items" as that term is defined at 48 CFR 2.101,
 * consisting of "Commercial Computer Software" and
 * "Commercial Computer Software Documentation", as such terms are
 * used in 48 CFR 12.212 or 48 CFR 227.7202-1 through 227.7202-4,
 * as applicable.
 */
package hitmap;

import ilog.views.chart.IlvChart;
import ilog.views.chart.data.IlvDataSet;
import ilog.views.chart.data.IlvDataSetPoint;
import ilog.views.chart.renderer.IlvSingleChartRenderer;
import ilog.views.chart.servlet.IlvDefaultChartHitmapDefinition;
import ilog.views.chart.servlet.IlvDefaultChartToolTip;
import ilog.views.chart.servlet.IlvIMapAttributes;

import java.util.List;

/**
 * The hitmap info definition for this demo.
 * @author Loki
 */
public class HitmapDefinition extends IlvDefaultChartHitmapDefinition {

  Override
  public IlvIMapAttributes getAttributes(final IlvChart chart, IlvSingleChartRenderer renderer, IlvDataSetPoint dp) {
    final IlvDataSet dataSet = dp.getDataSet();
    final int idx = dp.getIndex();
    
    SuppressWarnings("unchecked")
    final List<String> hrefs =
      (List<String>)dataSet.getProperty(HREFPropertyReader.HREFS_TAG);

    double allYValue = 0;
    double[] yValues = dataSet.getData().getYValues();
    for (int i=0; i<yValues.length; i++)
      allYValue+=yValues[i];

    return new IlvDefaultChartToolTip(dataSet.getDataLabel(idx), dataSet, idx) {

      Override
      public String getExtraAttributes() {
        StringBuffer extension = new StringBuffer();
        extension.append("function(){");
        extension.append("var r=new IlvAjax.HTMLRequest(view,view.updateImage);");
        extension.append("r.send(\'");
        extension.append("/js-chart-hitmap/ChartHitmap");
        extension.append("?request=image");
        extension.append("&width="+getClientWidth());
        extension.append("&height="+getClientHeight());
        extension.append("&explodedIdx=" + idx);
        extension.append("\');");
        extension.append("parent.frames[1].location='"+hrefs.get(idx).toString()+"';");
        extension.append("}");
        
        return extension.toString();
      }
    
    };
  }
  
  private String getClientWidth(){
    return "'+view.getWidth()+'";
  }
  
  private String getClientHeight(){
    return "'+view.getHeight()+'";
  }
}