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

Message ListMessage List     Post MessagePost Message

  Issue in display of plot using two level dynamic select options from database tables in Rails
Posted by Raj on Feb-04-2012 07:53
Attachments:
Hi,

I am facing problem in drawing plots from my database in Rails. Plot is not visible on the browser, even if in the rails log, it says ("sent data"). I am not sure about the problem but it seems related to page replace or data redirection. I am beginner for rails and chart director both.  I have attached the desktop screenshot too.

I will appreciate any help or clue.


-------------------------------------------------------
My Database is as follows:

clusters {id, Name}
nodes {id, cluster_id, name}
node_states{ id, node_id, state_id transition_time}

I want to create a dynamic select option menu to first choose Cluster and then Nodes in that cluster to Plot the state_id Vs transition_time plot for a specific node.

-----------------------------------

I am able to create two level dynamic select option using following code. I am plotting a default figure with node_id = '1' once I open the browser window. However, it is not able to refresh the figure with my final selection of node.
----------------------------------------------------------------------------
----- app/views/node_state_select/index.html.erb

<%= javascript_include_tag :defaults %>
  I want to obtain the State History data for the  Node
  <%= collection_select(:cluster, :cluster_id, Cluster.find(:all), :id, :name,
                      {:prompt => "Select a Cluster"},
                      {:onchange => "#{remote_function(:url  => {:action => "update_nodes"},
                                                       :with => "'cluster_id='+value")}"}) %>
  <div id="nodes"><%= render :partial => 'nodes', :object => @nodes %></div>
</div>


------- app/views/node_states_select/_node.html.erb

<form>
<%= collection_select(:node, :node_id, nodes, :id, :name,
                     {:prompt   => "Select a Node"},
                     {:onchange => "#{remote_function(:url => {:action => "list_node_state"},
                                                       :with => "'node_id='+value")}"}) %>
</form>
<img src="<%= url_for(:action => 'list_node_state', :node_id => (params[:node_id] || '1')) %>">
<br/>

---------------------------------------------------------------------------------

---------------- app/controller/node_states_select_controller.rb
  def update_nodes
       @cluster = Cluster.find(params[:cluster_id])
     nodes = Node.where("cluster_id = ?", @cluster)
       #nodes = @clusters.nodes

       render :update do |page|
         page.replace_html 'nodes', :partial => 'nodes', :object => nodes
       end
  end

  def list_node_state
    c = ChartDirector::XYChart.new(1000, 600, 0xeeeeee, 0x000000, 1)
    c.setPlotArea( 60, 20, 700, 400)
    c.yAxis().setTitle("State Change")
    c.xAxis().setTitle("Time ")
    c.xAxis().setLabelStep(3)

    selectedNode = params[:node_id]
    records = NodeState.where("node_id = ?", selectedNode).order("transition_time")
    time = []
    state = []
    records.each do |r|
      time.push(r.transition_time)
      state.push(r.valid_node_state_id)
    end

    c.addLineLayer(state)
    c.xAxis().setLabels(time)
    c.xAxis().setLabelStep(3)

    # Output the chart
    send_data(c.makeChart2(ChartDirector::PNG), :type => "image/png", :disposition => "inline")
  end

---------------------------------------------------------------------------
dynamicSelect.jpg

  Re: Issue in display of plot using two level dynamic select options from database tables in Rails
Posted by Peter Kwan on Feb-06-2012 23:39
Hi Raj,

In your browser, you have the following code:

{:onchange => "#{remote_function(:url => {:action => "list_node_state"},
                                                       :with => "'node_id='+value")}"})

The above code executes the list_node_state on the server side, and ignores the returned image. (Your code does not do anything with the returned image.) That is why nothing happens.

As far as I know, even if your code does not ignore the image, standard HTML/Javascript does not allow you to display the image. (There is no HTML/Javascript method to do that.)

For your case, the only method that can update the <IMG> tag is to update the SRC of the <IMG> tag. You can just use plain Javascript to modify the SRC attribute of the <IMG> tag to the URL you want it to use. There is no need to use #remote_function or any AJAX library.

Hope this can help.

Regards
Peter Kwan